# Fortran drill #4 â€“ Amicable numbers â€“ by Jacob

Document Sample

```					Fortran drill #4 – Amicable numbers – by Jacob Shapiro:
Source:
!Last change: _       15 Nov 104    9:55 pm
PROGRAM AmicableNumbers
IMPLICIT NONE
INTEGER:: n, h, t, s, i, AmicableOne, AmicableTwo
CHARACTER:: cAnswer = 'y'
CHARACTER:: IsPrime_h, IsPrime_t, IsPrime_s
WRITE(*,*) "Welcome to the Amicable Numbers tool"!
n=2
DO WHILE (cAnswer == 'y)'
!      First let's calculate h, t, and s.
h = 3 * ( 2 ** n      )-1
t = 3 * ( 2 ** (n -1) ) - 1
s = 9 * ( 2 ** (2*n-1)) - 1
!      Now make sure that h, t, and s are prime.
IsPrime_h = 't'
IsPrime_t = 't'
IsPrime_s = 't'
i=2
do WHILE (i< h)
if ((REAL(h)/i) - INT(REAL(h)/i) == 0 ) then
IsPrime_h = 'f'
exit
end if
i=i+1
end do
i=2
do WHILE (i< t)
if ((REAL(t)/i) - INT(REAL(t)/i) == 0 ) then
IsPrime_t = 'f'
exit
end if
i=i+1
end do
i=2
do WHILE (i< s)
if ((REAL(s)/i) - INT(REAL(s)/i) == 0 ) then
IsPrime_s = 'f'
exit
end if
i=i+1
end do
if ((IsPrime_h == 't') .AND. (IsPrime_t == 't') .AND. (IsPrime_s == 't')) then
!           All the numbers are prime, this means that our two amicable numbers are:
AmicableOne = (2 ** n) * h * t
AmicableTwo = (2 ** n) * s
WRITE(*,*) "The two amicable numbers I found are: (", AmicableOne, ",", AmicableTwo".)" ,
WRITE(*,*) "Should I find another Amicable Number? [y]es / [n]o"
end if
n=n+1
END DO

END PROGRAM AmicableNumbers
!References:
------------ !
!http://mathworld.wolfram.com/ThabitibnKurrahRule.html
!http://www.shyamsundergupta.com/amicable.htm

Output:

```
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
 views: 13 posted: 6/13/2010 language: English pages: 2
How are you planning on using Docstoc?