Task: Amicable pairs


C++: #include <vector>
#include <unordered_map>
#include <iostream>

int main() {
    std::vector<int> alreadyDiscovered;
    std::unordered_map<int, int> divsumMap; 
    int count = 0;

    for (int N = 1; N <= 20000; ++N)
    {      
        int divSumN = 0;

        for (int i = 1; i <= N / 2; ++i)
        {
            if (fmod(N, i) == 0)
            {
                divSumN += i;
            }
        }
        
        
        if (divSumN != 1) 
            divsumMap[N] = divSumN;

        for (std::unordered_map<int, int>::iterator it = divsumMap.begin(); it != divsumMap.end(); ++it)
        {
            int M = it->first;
            int divSumM = it->second;
            int divSumN = divsumMap[N];

            if (N != M && divSumM == N && divSumN == M)
            {
                
                if (std::find(alreadyDiscovered.begin(), alreadyDiscovered.end(), N) != alreadyDiscovered.end())
                    break;

                std::cout << "[" << M << ", " << N << "]" << std::endl;

                alreadyDiscovered.push_back(M);
                alreadyDiscovered.push_back(N);
                count++;
            }
        }
    }

    std::cout << count << " amicable pairs discovered" << std::endl;
}









Fortran:       MODULE FACTORSTUFF	
Concocted by R.N.McLean, MMXV.
       INTEGER LOTS,ILIMIT		
       PARAMETER (ILIMIT = 2147483647)	
       PARAMETER (LOTS = 22000)	
       INTEGER KNOWNSUM(LOTS)		
       CONTAINS			
        INTEGER FUNCTION SUMF(N)	
         INTEGER N			
         INTEGER S,F,F2,INC,BOOST	
          IF (N.LE.LOTS) THEN		
            SUMF = KNOWNSUM(N)			
           ELSE			
Could use SUMF in place of S, but some compilers have been confused by such usage.
            S = 1			
            F = 1			
            INC = 1			
            IF (MOD(N,2) .EQ. 1) INC = 2
    1       F = F + INC			
            F2 = F*F				
            IF (F2 .LT. N) THEN			
              IF (MOD(N,F) .EQ. 0) THEN		
                BOOST = F + N/F			
                IF (S .GT. ILIMIT - BOOST) GO TO 666	
                S = S + BOOST			
              END IF				
              GO TO 1				
            END IF			
            IF (F2 .EQ. N) THEN	
              IF (S .GT. ILIMIT - F) GO TO 666	
              S = S + F			
            END IF			
            SUMF = S			
          END IF			
         RETURN			
Cannot calculate the sum, because it exceeds the integer limit.
  666     SUMF = -666		
        END FUNCTION SUMF	
         SUBROUTINE PREPARESUMF	
Convert the Sieve of Eratoshenes to have each slot contain the sum of the proper divisors of its slot number.
Changes to instead count the number of factors, or prime factors, etc. would be simple enough.
         INTEGER F		
          KNOWNSUM(1) = 0		
          KNOWNSUM(2:LOTS) = 1		
          DO F = 2,LOTS/2		
            FOR ALL(I = F + F:LOTS:F) KNOWNSUM(I) = KNOWNSUM(I) + F	
          END DO			
        END SUBROUTINE PREPARESUMF	
      END MODULE FACTORSTUFF	
       PROGRAM AMICABLE		
       USE FACTORSTUFF		
       INTEGER I,N		
       INTEGER S1,S2		
        CALL PREPARESUMF		
c        WRITE (6,66) (I,KNOWNSUM(I), I = 1,48)
c   66   FORMAT (10(I3,":",I5,"|"))
        DO N = 2,20000		
          S1 = SUMF(N)			
          IF (S1 .EQ. N) THEN		
            WRITE (6,*) "Perfect
          ELSE IF (S1 .GT. N) THEN	
            S2 = SUMF(S1)		
            IF (S2.EQ.N) WRITE (6,*) "Amicable
          END IF			
        END DO			
      END