Fortran: Creating a sequence with constraints

Click For Summary

Discussion Overview

The discussion revolves around creating a Fortran program that generates a sequence of numbers based on specific mathematical constraints. The sequence is defined such that if a number is even, the next number is half of it, and if it is odd, the next number is calculated as 3 times the number plus one. The sequence terminates when it reaches the number 1. Participants are sharing their coding attempts, challenges, and suggestions for improvement.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant expresses confusion about how to implement the sequence logic using DO, IF, and MOD statements in Fortran.
  • Another participant suggests starting with an algorithm in plain English to clarify the steps before coding.
  • A participant shares a code snippet but acknowledges it is incorrect and outlines their understanding of the sequence logic.
  • There is a suggestion to use a DO WHILE loop instead of a DO loop for better control over the sequence execution.
  • A question arises about the need for a logical statement in the context of the loop.
  • A participant reports that their updated code only outputs repeated values and does not terminate correctly when reaching 1.
  • Another participant points out that the issue is due to not updating the variable x with the new calculated values in each iteration.
  • A participant seeks advice on how to implement a termination condition based on the length of the sequence.
  • One participant advises initializing a counter to track the number of terms in the sequence and using an EXIT statement to terminate the loop if the count exceeds a specified limit.

Areas of Agreement / Disagreement

Participants generally agree on the need to properly implement the sequence logic and the use of loops and conditionals in Fortran. However, there are varying approaches and suggestions on how to structure the code effectively, indicating that the discussion remains unresolved regarding the best coding practices.

Contextual Notes

Limitations include potential misunderstandings of Fortran syntax and logic flow, as well as the need for clarity on how to manage variable updates within loops. There is also an unresolved aspect regarding the implementation of a termination condition based on the sequence length.

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 ·
Replies
20
Views
4K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 17 ·
Replies
17
Views
7K
  • · Replies 37 ·
2
Replies
37
Views
5K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 11 ·
Replies
11
Views
12K