Fortran: Creating a sequence with constraints

In summary,The user is trying to write a program in Fortran that will create a sequence of numbers (starting with a number that they input) that follow the given 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.The user is aware that they should first write their own code first and then try to get help from that point, but they seem to be having trouble putting it all together. They are aware that the DO, IF
  • #1
d-star
8
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
  • #2
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"...
 
  • #3
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
 
  • #4
don't do do, do while

do while (.true.)
.
.
.
if (x<2) exit
end do
 
  • #5
In this case, would I have to declare a logical statement?
 
  • #6
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
 
  • #7
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
 
  • #8
got it! thanks so much!
 
  • #9
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
 

What is Fortran?

Fortran is a high-level programming language that was developed specifically for scientific and engineering applications. It is widely used for creating complex mathematical and scientific calculations.

What is a sequence in Fortran?

In Fortran, a sequence is a series of instructions or statements that are executed in a specific order. It is used to control the flow of a program and to perform specific tasks.

What are constraints in Fortran?

Constraints in Fortran refer to the limitations or conditions that are placed on a sequence. These can include restrictions on the values that can be used, the order in which statements are executed, or the number of times a statement can be repeated.

How do I create a sequence with constraints in Fortran?

To create a sequence with constraints in Fortran, you will need to use control structures such as DO loops, IF statements, and GOTO statements. These can help you control the flow of your program and impose the necessary constraints on your sequence.

What are some common constraints used in Fortran sequences?

Some common constraints used in Fortran sequences include limiting the number of iterations in a DO loop, checking for specific conditions using IF statements, and using GOTO statements to jump to specific parts of the sequence. Constraints can also be imposed on the data types and values used in the sequence.

Similar threads

  • Programming and Computer Science
Replies
10
Views
993
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
20
Views
3K
  • Programming and Computer Science
2
Replies
37
Views
3K
  • General Math
Replies
8
Views
1K
  • Programming and Computer Science
Replies
7
Views
6K
Replies
1
Views
533
  • General Math
Replies
1
Views
1K
Back
Top