Fortran Fortran: Creating a sequence with constraints

AI Thread Summary
The discussion focuses on creating a Fortran program to generate a number sequence based on specific constraints: if a number is even, the next number is n/2; if odd, it is 3n+1, terminating when reaching 1. The user initially struggles with implementing the logic using DO, IF, and MOD statements, leading to incorrect outputs that repeat the first input value. After receiving guidance, the user learns to assign the new value of x within the loop to ensure the sequence progresses correctly. Additionally, they seek advice on terminating the sequence if it exceeds a certain length, which involves using a counter and an EXIT statement. The conversation emphasizes the importance of correctly updating the variable and managing loop conditions for successful implementation.
d-star
Messages
8
Reaction score
0
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!
 
Technology news on Phys.org
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
 
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.
 
  • #10
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
 

Similar threads

Replies
20
Views
3K
Replies
8
Views
4K
Replies
17
Views
6K
Replies
2
Views
2K
Replies
10
Views
2K
Replies
12
Views
2K
Replies
11
Views
11K
Back
Top