Project Euler #21: Amicable numbers

Sort by

recency

|

65 Discussions

|

  • + 0 comments

    The amicable numbers are a well-studied sequecence, and a list of several of them can be found here. Technically, you could just use this list to find your result, but I encourage you to use them only for checking and testing your code. Otherwise, what's the point?

  • + 1 comment
    def sumof(n):
        some=0
        for i in range(1,n):
            if n%i==0:
                some+=i
        return some
            
    
    
    def is_amicable(a,b):
        return  (sumof(a)==b) and ((sumof(b))==a) and a!=b
            
    
    
    
    
    t = int(input())
    for _ in range(t):
        n = int(input())
        you=0
        for i in range(1,n+1):
            for j in range(i+1,n+1):
                if (is_amicable(i,j)):
                    you+=(i+j)
        print(you)
    
  • + 0 comments

    // C# using System; using System.Collections.Generic;

    class Program { static int S(int n) { int sum = 1;

        for (int x = 2; x <= Math.Sqrt(n); x++)
        {
            if (n % x == 0)
            {
                sum += x + n / x;
            }
        }
    
        return sum;
    }
    
    static void Main()
    {
        int numTestCases = int.Parse(Console.ReadLine());
        List<int> testCases = new List<int>();
    
        for (int i = 0; i < numTestCases; i++)
        {
            testCases.Add(int.Parse(Console.ReadLine()));
        }
    
        HashSet<int> result = new HashSet<int>();
    
        for (int x = 0; x <= testCases.Max(); x++)
        {
            if (x == S(S(x)) && x != S(x))
            {
                result.Add(x);
            }
        }
    
        foreach (int testCase in testCases)
        {
            Console.WriteLine(result.Where(x => x <= testCase).Sum());
        }
    }
    

    }

  • + 0 comments
    #python
    def s(n):
        s=1
        for x in range(2, int(n**.5)+1):
            if n %x==0:
                s+=x+n//x
        return s
    n=[int(input()) for x in range(int(input()))]
    res=set()
    for x in range(max(n)+1):
        if x==s(s(x)) and x!=s(x):
            res.add(x)
    for _ in n:
        print(sum(x for x in res if x<=_))
    
  • + 0 comments

    easy sol

    def divi_sum(n):
        r_sum=0
        for i in range(1,int(n**0.5)+1):
            if n%i==0:
                r_sum+=i+n//i
        if int(n**0.5)==n**0.5:
            r_sum+=-int(n**0.5)
        r_sum+=-n
        return r_sum
    
    req_numbers={}
    our_sum=0
    for i in range(1,10**5):
        req_numbers[i]=our_sum
        x=divi_sum(i)
        if x!=i and i==divi_sum(x):
            our_sum+=i
            
    for _ in range(int(input())):
        no=int(input())
        print(req_numbers[no])