Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Fortran: Creating a sequence with constraints

  1. Oct 6, 2012 #1
    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!
     
  2. jcsd
  3. Oct 6, 2012 #2

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    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"....
     
  4. Oct 6, 2012 #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
     
  5. Oct 6, 2012 #4
    don't do do, do while

    do while (.true.)
    .
    .
    .
    if (x<2) exit
    end do
     
  6. Oct 7, 2012 #5
    In this case, would I have to declare a logical statement?
     
  7. Oct 7, 2012 #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
     
  8. Oct 7, 2012 #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
     
  9. Oct 8, 2012 #8
    got it! thanks so much!
     
  10. Oct 8, 2012 #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.
     
  11. Oct 8, 2012 #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
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook