Matlab help?! Write a matlab code to prove (or disprove) the Collatz Conjecture. Run the code for a few different values of n, and make note of the results. Did n always eventually go to 1? Tips: You will need to use a while loop. While your integer, n, is greater than 1, you want to continue doing the calculation (i.e. n/2 if even and 3n+1 if odd). Once n reaches 1 (or something less?), you want the program to end. Within the while loop, an if statement is necessary to determine which calculation you should do, depending on if n is even or odd at the time( by using the mod function).
while (n> 1) if (mod(n,2)) % this is odd if 1 (true) disp('ODD'); else disp('EVEN'); end; end; % while n>1 disp('n less than or equal to 1');
is this what you want ? n=5 while (n> 1) if (mod(n,2)) % this is odd %disp('ODD'); n= 3*n+1 else %disp('EVEN'); n= n/2 end end; % while n>1
Let me check it in matlab, hold on (:
perfect (: <3
Please e-mail me your 'A'
my 'A'?
Join our real-time social learning platform and learn together with your friends!