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

Fortran: Searching through a list of numbers and finding which numbers meet criteria

  1. Oct 21, 2012 #1
    Hi, I'm writing a program in fortran that basically creates a loop from 1 until a certain number x (input by the user), and goes through each value between 1 and the certain number x in order to determine if each value meets certain criteria. The criteria is that the sum of the square of the individual digits created by each value in between 1 and x (where each value is taken from the preceding sum), must eventually equal 1. For example, the number 7 meets this criteria, because when we take the sum of the square of the individual digits, (7^2)=49, (4^2)+(9^2)=97, (9^2)+(7^2)=130, (1^2)+(3^2)+(0^2)=10, (1^2)+(0^2)= 1. The same goes for the number 13, except this reaches 1 after only two steps: (1^2)+(3^2)=10, (1^2)+(0^2)= 1.
    I have already wrote the program for calculating the sum of the square of the individual digits of a number, and eventually reaching 1. Although I am having trouble finding a function to use in order to go through each number from 1 to a certain value x, and determining whether each number in between 1 and x meets this criteria or not.
    Any help would be appreciated! Thanks!
     
  2. jcsd
  3. Oct 22, 2012 #2

    Mark44

    Staff: Mentor

    Re: Fortran: Searching through a list of numbers and finding which numbers meet crite

    What have you tried?
     
  4. Oct 22, 2012 #3
    Re: Fortran: Searching through a list of numbers and finding which numbers meet crite

    You are having trouble finding a function to use? You already wrote it! Just make a formal function out of your code and call it from within the loop of n = 1, x
     
  5. Oct 23, 2012 #4
    Re: Fortran: Searching through a list of numbers and finding which numbers meet crite

    alright, thanks! it actually had a suspicion that that was the case. Although I have another question as well.
    I also need to integrate a way that basically identifies if each value from 1 to x satisfies the condition that its sequence eventually reaches 1, and I also need to make my program identify if the sequence does not reach 1 (goes on forever). So if the number reaches 1 it is TRUE, and if does not reach 1 (goes on forever) then it is FALSE. I then need to be able to list all the numbers between 1 and x that are TRUE(sequence reaches 1). So far I have:

    PROGRAM S
    IMPLICIT NONE

    INTEGER :: x,j,a
    READ (*,*) x
    j = 1

    DO a = 1,x
    CALL S
    j = j +1
    END DO

    END PROGRAM S2


    (where S is my function that solves for the square of the sum of the individual digits from 1 to x, determining if each number's sequence reaches 1 or not)

    Any suggestions? Thanks!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Fortran: Searching through a list of numbers and finding which numbers meet criteria
Loading...