Find the sum of the all amicable numbers up to N
Last Updated :
23 Jun, 2022
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++
#include <bits/stdc++.h>
using namespace std;
#define N 100005
set< int > AMICABLE()
{
int sum[N];
memset (sum, 0, sizeof sum);
for ( int i = 1; i < N; i++) {
sum[i]++;
for ( int j = 2; j * j <= i; j++) {
if (i % j == 0) {
sum[i] += j;
if (i / j != j)
sum[i] += i / j;
}
}
}
set< int > s;
for ( int i = 2; i < N; i++) {
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;
}
int SumOfAmicable( int n)
{
int sum = 0;
set< int > s = AMICABLE();
for ( auto i = s.begin(); i != s.end(); ++i) {
if (*i <= n)
sum += *i;
else
break ;
}
return sum;
}
int main()
{
int n = 284;
cout << SumOfAmicable(n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static final int N= 100005 ;
static Set<Integer> AMICABLE()
{
int sum[] = new int [N];
for ( int i = 0 ; i < N; i++)
sum[i]= 0 ;
for ( int i = 1 ; i < N; i++)
{
sum[i]++;
for ( int j = 2 ; j * j <= i; j++)
{
if (i % j == 0 )
{
sum[i] += j;
if (i / j != j)
sum[i] += i / j;
}
}
}
Set<Integer> s = new HashSet<Integer>();
for ( int i = 2 ; i < N; i++)
{
if (i != sum[i] && sum[i] < N && i == sum[sum[i]])
{
s.add(i);
s.add(sum[i]);
}
}
return s;
}
static int SumOfAmicable( int n)
{
int sum = 0 ;
Set<Integer> s = AMICABLE();
for (Integer x : s)
{
if (x <= n)
sum += x;
}
return sum;
}
public static void main(String args[])
{
int n = 284 ;
System.out.println( SumOfAmicable(n));
}
}
|
Python3
import math as mt
N = 100005
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))):
if (i % j = = 0 ):
Sum [i] + = j
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
def SumOfAmicable(n):
Sum = 0
s = AMICABLE()
s = sorted (s)
for i in s:
if (i < = n):
Sum + = i
else :
break
return Sum
n = 284
print (SumOfAmicable(n))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static readonly int N = 100005;
static HashSet< int > AMICABLE()
{
int []sum = new int [N];
for ( int i = 0; i < N; i++)
sum[i] = 0;
for ( int i = 1; i < N; i++)
{
sum[i]++;
for ( int j = 2; j * j <= i; j++)
{
if (i % j == 0)
{
sum[i] += j;
if (i / j != j)
sum[i] += i / j;
}
}
}
HashSet< int > s = new HashSet< int >();
for ( int i = 2; i < N; i++)
{
if (i != sum[i] && sum[i] < N &&
i == sum[sum[i]])
{
s.Add(i);
s.Add(sum[i]);
}
}
return s;
}
static int SumOfAmicable( int n)
{
int sum = 0;
HashSet< int > s = AMICABLE();
foreach ( int x in s)
{
if (x <= n)
sum += x;
}
return sum;
}
public static void Main()
{
int n = 284;
Console.WriteLine( SumOfAmicable(n));
}
}
|
PHP
<?php
$N = 10005;
function AMICABLE()
{
global $N ;
$sum = array_fill (0, $N , 0);
for ( $i = 1; $i < $N ; $i ++)
{
$sum [ $i ]++;
for ( $j = 2; $j * $j <= $i ; $j ++)
{
if ( $i % $j == 0)
{
$sum [ $i ] += $j ;
if ( $i / $j != $j )
$sum [ $i ] += $i / $j ;
}
}
}
$s = array ();
for ( $i = 2; $i < $N ; $i ++)
{
if ( $i != $sum [ $i ] and $sum [ $i ] < $N and
$i == $sum [ $sum [ $i ]] and
!in_array( $i , $s ) and
!in_array( $sum [ $i ], $s ))
{
array_push ( $s , $i );
array_push ( $s , $sum [ $i ]);
}
}
return $s ;
}
function SumOfAmicable( $n )
{
$sum = 0;
$s = AMICABLE();
$s = array_unique ( $s );
sort( $s );
for ( $i = 0; $i != count ( $s ); ++ $i )
{
if ( $s [ $i ] <= $n )
$sum += $s [ $i ];
else
break ;
}
return $sum ;
}
$n = 284;
echo SumOfAmicable( $n );
?>
|
Javascript
<script>
let N=100005;
function AMICABLE()
{
let sum = new Array(N);
for (let i = 0; i < N; i++)
sum[i]=0;
for (let i = 1; i < N; i++)
{
sum[i]++;
for (let j = 2; j * j <= i; j++)
{
if (i % j == 0)
{
sum[i] += j;
if (i / j != j)
sum[i] += i / j;
}
}
}
let s = new Set();
for (let i = 2; i < N; i++)
{
if (i != sum[i] && sum[i] < N && i == sum[sum[i]])
{
s.add(i);
s.add(sum[i]);
}
}
return s;
}
function SumOfAmicable(n)
{
let sum = 0;
let s = AMICABLE();
for (let x of s.values())
{
if (x <= n)
sum += x;
}
return sum;
}
let n = 284;
document.write( SumOfAmicable(n));
</script>
|
Time Complexity: O(n2)
Auxiliary Space: O(100005)
Share your thoughts in the comments
Please Login to comment...