## Fortran: Creating a sequence with constraints

I'm trying to write a program in fortran that will create a sequence of numbers (starting with a number that I input) that follow the following constraints: If the number (n) in the sequence is even, then the next number in the sequence will be n/2. If the number (n) in the sequence is odd, then the next number in the sequence is 3n+1. When the sequence reaches the number 1, the sequence terminates.
I am aware that I should first write my own code first and then try to get help from that point, but I seem to be having trouble putting it all together. I am aware that the DO, IF, and MOD(x,y) statements need to be used, but I am confused as to how to arrange them so that the given constraints will work.
Thanks!
 PhysOrg.com science news on PhysOrg.com >> City-life changes blackbird personalities, study shows>> Origins of 'The Hoff' crab revealed (w/ Video)>> Older males make better fathers: Mature male beetles work harder, care less about female infidelity
 Recognitions: Science Advisor If you are getting overwhelmed by all the details of Fortran, start by writing a set of instructions (an algorithm) in English to solve the problem. We aren't going to do your homework for you unless YOU show some effort, here, but obviously the first step is "Read in the first value of n from the user"....
 So I managed to construct something. Obviously, it isn't correct, but I think I might be on the right track. Before I write the code I'll do as you said, explain what I want to do in simple english: "Read the value that I input, and start the sequence with that value" "Starting from the value that was inputted (ie. it is the first value in the sequence) , follow the given constraints for each value in the sequence" "Following the given constraints, when the sequence reaches the value 1, terminate sequence" So I will write the code that I managed to whip up: PROGRAM Number IMPLICIT NONE INTEGER :: N REAL :: x READ (*,*) b DO x = b, (?) <---(not exactly sure what to put here) IF (MOD(x,2)==0) THEN WRITE (*,*) x*0.5 ELSE WRITE (*,*) 3*x+1 IF (x<2) EXIT END IF END DO END PROGRAM Number

## Fortran: Creating a sequence with constraints

don't do do, do while

do while (.true.)
.
.
.
if (x<2) exit
end do
 In this case, would I have to declare a logical statement?
 Okay, so I managed to whip up a new code that seems to be working better than the previous one. The problem is, when I run the program, the sequence only follows one of the constraints, and keeps repeating from the first number that I input. This may sound confusing, but for example, if I input 2, it outputs 1,1,1,1,1... Or if I input 9, it outputs 28,28,28,28,... instead of being 9,28,14,7,.. And the sequence does not seem to terminate when it reaches 1. How can I fix this problem? Here is the code: PROGRAM Number IMPLICIT NONE REAL :: x READ (*,*) x DO while (.true.) IF (MOD(x,2)==0) THEN WRITE (*,*) x*0.5 ELSE WRITE (*,*) 3*x+1 IF (x<2) EXIT END IF END DO END PROGRAM Number
 That's because you are only printing the intended new value for x, but you never assign it to it for use in the next iteration x = 3*x+1 write(*,*) x the same for the other clause of the IF statement x = x*0.5 write(*,*) x
 got it! thanks so much!
 Sorry! I have one more quick question! I would like to configure my program so that if the length of the sequence (number of terms) exceeds a certain number, the sequence terminates. I know that I must use an EXIT statement, as well as defining the number of terms under a certain variable, although I am not sure how to put it all together.
 initialize a counter before entering the loop, increment it by one in the loop, test its value before end of loop...if too large, "exit" loop, literally, use exit

 Tags fortran, sequence

 Similar discussions for: Fortran: Creating a sequence with constraints Thread Forum Replies Programming & Comp Sci 1 Programming & Comp Sci 0 Programming & Comp Sci 3 Programming & Comp Sci 1