Amicable pair of integers function
1. Definition
Amicable pair of numbers :
A pair of numbers, (p, q) is amicable if :
- the sum of the proper divisors p is q , and
- the sum of the proper divisors q is p
The following program gives, if it exists, the amicable
number of a given integer.
2. Example in Fortran90 language:
! Main program:
! -------------------
!
PROGRAM amicable
IMPLICIT NONE
INTEGER :: i, j, n, sum1, sum2
PRINT*,"\n Enter an integer : --> "
READ*, n
sum1 = 0
do i = 1, n-1
if (mod(n,i) == 0) THEN
sum1 = sum1 + i
end IF
continue
end do
sum2 = 0
do j = 1, sum1-1
if (mod(sum1,j) == 0) THEN
sum2 = sum2 + j
end IF
continue
end do
if (n == sum2) then
PRINT*,"The number", n," has an amicable number. It is: ", sum1,"."
else
PRINT*,"The number", n, " doesn't have an amicable number."
endif
END PROGRAM amicable
! ----------------------------------------
Compiled with SciTE, yields:
----------------------------
>g77 -x f77 -ffree-form -W -Wall "amicable.f90" -o "amicable.exe"
>Exit code: 0
Executed, gives:
----------------
C:\Fortran90>amicable
Enter an integer : -->
55
The number 55 doesn't have an amicable number.
C:\Fortran90>amicable
Enter an integer : -->
220
The number 220 has an amicable number. It is: 284.
C:\Fortran90>