Find the sum of the all amicable numbers up to N

Given a number N. FInd the sum of he all amicable numbers up to N. If A and B are Amicable pairs (Two numbers are amicable if the first is equal to the sum of divisors of the second) then A and B are called as Amicable numbers.

Examples:

Input : 284
Output : 504
Explanation : 220 and 284 are two amicable numbers upto 284

Input : 250
Output : 220
Explanation : 220 is the only amicable number



Approach :
An efficient approach is to store all amicable numbers in a set and for a given N sum all the numbers in the set which are less than equals to N.

Below code is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find sum of all
// amicable numbers up to n
#include <bits/stdc++.h>
using namespace std;
  
#define N 100005
  
// Function to return all amicable numbers
set<int> AMICABLE()
{
    int sum[N];
    memset(sum, 0, sizeof sum);
    for (int i = 1; i < N; i++) {
  
        // include 1
        sum[i]++;
  
        for (int j = 2; j * j <= i; j++) {
  
            // j is proper divisor of i
            if (i % j == 0) {
                sum[i] += j;
  
                // if i is not a perfect square
                if (i / j != j)
                    sum[i] += i / j;
            }
        }
    }
  
    set<int> s;
    for (int i = 2; i < N; i++) {
  
        // insert amicable numbers
        if (i != sum[i] and sum[i] < N and i == sum[sum[i]]
            and !s.count(i) and !s.count(sum[i])) {
            s.insert(i);
            s.insert(sum[i]);
        }
    }
  
    return s;
}
  
// function to find sum of all
// amicable numbers up to N
int SumOfAmicable(int n)
{
    // to store required sum
    int sum = 0;
  
    // to store all amicable numbers
    set<int> s = AMICABLE();
  
    // sum all amicable numbers upto N
    for (auto i = s.begin(); i != s.end(); ++i) {
        if (*i <= n)
            sum += *i;
        else
            break;
    }
  
    // required answer
    return sum;
}
  
// Driver code to test above functions
int main()
{
    int n = 284;
  
    cout << SumOfAmicable(n);
  
    return 0;
}

chevron_right


Python3

# Python3 program to findSum of all
# amicable numbers up to n
import math as mt

N = 100005

# Function to return all amicable numbers
def AMICABLE():

Sum = [0 for i in range(N)]

for i in range(1, N):
Sum[i] += 1

for j in range(2, mt.ceil(mt.sqrt(i))):

# j is proper divisor of i
if (i % j == 0):
Sum[i] += j

# if i is not a perfect square
if (i // j != j):
Sum[i] += i // j

s = set()
for i in range(2, N):
if(i != Sum[i] and Sum[i] < N and i == Sum[Sum[i]] and i not in s and Sum[i] not in s): s.add(i) s.add(Sum[i]) return s # function to findSum of all amicable # numbers up to N def SumOfAmicable(n): # to store requiredSum Sum = 0 # to store all amicable numbers s = AMICABLE() #Sum all amicable numbers upto N s = sorted(s) for i in s: if (i <= n): Sum += i else: break # required answer return Sum # Driver Code n = 284 print(SumOfAmicable(n)) # This code is contributed by # mohit kumar 29 [tabbyending]

Output:

504


My Personal Notes arrow_drop_up

pawanasipugmailcom

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : mohit kumar 29