next up previous
Next: About this document ... Up: CS101 Lecture 5 Previous: Top Shell for test235

Now for Fortran

! A Naive solution of the 235-problem.
!    Coded by Siva (siva@cse.iitb.ernet.in) on Aug 6, 2000
Program p235
!
! first declare all the variables you need
      Integer :: which, counter, current
! and the function types
      Logical :: test235
! To Keep track of CPU usage use these two vars.
      Real :: tstart, tend
! Ask the user which number he wants.
      Write (*,*) "Find which-th number? -> "
      Read (*,*) which
! Initialize counter
      counter = 0
! start from 1
      current = 1
! set start timer
      Call CPU_TIME (tstart)
!keep going until we get enough numbers
      Do
! have we found enough numbers. if yes stop trying more.
         If (counter == which) Exit
! check current number. if it is a 235-number increment counter.
         If (test235(current)) counter = counter + 1
! move to next number
         current = current + 1
      End Do
! check how much cpu time has elapsed
      Call CPU_TIME (tend)
! show the user the answer
      Write (*,*) "The ", which, "-th 235-number is: ", current - 1
      Write (*,*) "It took ", tend - tstart, " seconds of CPU time."
End Program p235

Logical Function test235 (n)
! Purpose: We take a number n as input and test it.
! We return true if it is a 235-number. else false.
!
! define the argument
      Integer, Intent (In) :: n
!
! we divide repeatedly by 2 first, 3 second and 5 last
! while the number is divisible by 2, 3, 5
! Use variable quot to store the quotient
      Integer :: quot
!
      quot = n
!
! repeatedly divide by 2 while number is even
      Do
         If (Mod(quot, 2) < > 0) Exit
         quot = quot / 2
      End Do
!
! then by 3 while it is divisble
      Do
         If (Mod(quot, 3) < > 0) Exit
         quot = quot / 3
      End Do
!
! finally by 5 while it is divisible
      Do
         If (Mod(quot, 5) < > 0) Exit
         quot = quot / 5
      End Do
!
      test235 = (quot == 1)
!
End Function test235



G. Sivakumar 2000-08-06