## Matlab

Moderators: None (Apply to moderate this forum)
Number of posts: 2174

This Forum Only

loop HELP "simple" Posted by jmed on 18 Feb 2010 at 2:08 PM
Generate a sequence of numbers. Start with integer n. If n is even divide by 2. If n is odd, multiply by 3 and add 1. Repeat process with new value of n terminating when n = 1.

Ex. n = 11 would produce

11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
Re: loop HELP "simple" Posted by jjasso5 on 18 Feb 2010 at 5:43 PM
x = input('Give me an integer: ');
while x ~= 1
if rem (x, 2) == 0
x = x/2
else
x = x*3 + 1
end
end
Re: loop HELP "simple" Posted by jmed on 18 Feb 2010 at 5:58 PM
It also says to save the generated sequence of numbers into a vector named seq. Display the variable seq.

EX:

>>project5
n = 3

seq =

3 10 5 16 8 4 2 1
Re: loop HELP "simple" Posted by jmed on 18 Feb 2010 at 5:58 PM
It also says to save the generated sequence of numbers into a vector named seq. Display the variable seq.

EX:

>>project5
n = 3

seq =

3 10 5 16 8 4 2 1

## Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic