Project Euler #23: Non-abundant sums

Sort by

recency

|

48 Discussions

|

  • + 0 comments

    JavaCode

    import java.util.Scanner;
    
    public class Solution {
            public static int sumOfDivisors(int n) {
            int sum = 1; 
            int sqrt = (int) Math.sqrt(n);
            
            for (int i = 2; i <= sqrt; i++) {
                if (n % i == 0) {
                    if (i == n / i) {
                        sum += i; 
                    } else {
                        sum += i + (n / i);
                    }
                }
            }
            
            return sum;
        }
    
        public static void main(String[] args) {
            Scanner s = new Scanner(System.in);
            int testcase = s.nextInt(); 
    
            while (testcase-- > 0) {
                int n = s.nextInt(); 
                boolean[] isAbundant = new boolean[n + 1];
                for (int i = 1; i <= n; i++) {
                    if (sumOfDivisors(i) > i) {
                        isAbundant[i] = true;
                    }
                }
                
                boolean canBeExpressed = false;
                for (int i = 1; i <= n / 2; i++) {
                    if (isAbundant[i] && isAbundant[n - i]) {
                        canBeExpressed = true;
                        break;
                    }
                }
                
                if (canBeExpressed) {
                    System.out.println("YES");
                } else {
                    System.out.println("NO");
                }
            }
            
            s.close();
        }
    }
    
  • + 0 comments

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

    class Result { public static bool IsAbundant(int n) { int sum = 1; // 1 is a proper divisor for all numbers for (int i = 2; i * i <= n; i++) { if (n % i == 0) { sum += i; if (i != n / i) // avoid counting the same divisor twice for perfect squares sum += n / i; } } return sum > n; }

    public static bool CanBeExpressedAsSumOfTwoAbundantNumbers(int n)
    {
        for (int i = 12; i <= n / 2; i++)
        {
            if (IsAbundant(i) && IsAbundant(n - i))
                return true;
        }
        return false;
    }
    

    }

    class Solution { public static void Main(string[] args) { int t = Convert.ToInt32(Console.ReadLine().Trim()); List results = new List();

        for (int tItr = 0; tItr < t; tItr++)
        {
            int n = Convert.ToInt32(Console.ReadLine().Trim());
            results.Add(Result.CanBeExpressedAsSumOfTwoAbundantNumbers(n) ? "YES" : "NO");
        }
    
        Console.WriteLine(string.Join("\n", results));
    }
    

    }

  • + 0 comments

    c# using System; using System.Collections.Generic;

    public class Program { static List primes = new List { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47 }; static List abundants = new List();

    static long Mod(long x, long mod)
    {
        return x % mod >= 0 ? x % mod : x % mod + mod;
    }
    
    static List<int> FindPrimes(int n)
    {
        for (int i = 51; i <= n; i += 2)
        {
            bool flag = false;
            foreach (int j in primes)
            {
                if (j * j > i)
                    break;
                if (i % j == 0)
                {
                    flag = true;
                    break;
                }
            }
            if (!flag)
                primes.Add(i);
        }
        return primes;
    }
    
    static bool IsAbundant(int n)
    {
        long ans = 1;
        int n_copy = n;
        foreach (int i in primes)
        {
            if (i * i > n_copy)
                break;
            if (n % i == 0)
            {
                int cnt = 0;
                while (n % i == 0)
                {
                    cnt++;
                    n /= i;
                }
                ans *= ((Power(i, cnt + 1) - 1) / (i - 1));
            }
        }
        if (n > 1)
            ans *= ((n * n - 1) / (n - 1));
        ans -= n_copy;
        return ans > n_copy;
    }
    
    static bool Solve(int n)
    {
        foreach (int i in abundants)
        {
            if (i > n / 2 + 1)
                return false;
            if (abundants.BinarySearch(n - i) >= 0)
            {
                return true;
            }
        }
        return false;
    }
    
    static long Power(long x, long y)
    {
        long ans = 1;
        while (y > 0)
        {
            if ((y & 1) == 1)
            {
                ans = ans * x;
            }
            x = x * x;
            y >>= 1;
        }
        return ans;
    }
    
    public static void Main(string[] args)
    {
        primes.Capacity = 39;
        FindPrimes(168);
        abundants.Capacity = 25053;
        for (int i = 2; i <= 28123; i++)
        {
            if (IsAbundant(i))
                abundants.Add(i);
        }
    
        int t;
        string input = Console.ReadLine();
        if (input != null)
        {
            t = int.Parse(input);
            while (t-- > 0)
            {
                string line = Console.ReadLine();
                if (line != null)
                {
                    int n = int.Parse(line);
                    if (n > 28123)
                    {
                        Console.WriteLine("YES");
                        continue;
                    }
                    if (Solve(n))
                        Console.WriteLine("YES");
                    else
                        Console.WriteLine("NO");
                }
            }
        }
    }
    

    }

  • + 0 comments
    #python
    def divsum(n):
        s=1
        for x in range(2, int(n**.5)+1):
            if n %x==0:
                s+=x
                if n//x!=x:
                    s+=n//x
        return s
    res=set()
    for x in range(12, 28123):
        if x<divsum(x):
            res.add(x)
    def check(n):
        if n>28123:
            return "YES"
        elif n/2 in res:
            return "YES"
        for x in res:
            if n-x in res:
                return "YES"
        return "NO"
    t=int(input())
    for x in range(t):
        n=int(input())
        print(check(n))
    
  • + 0 comments
    abundant=[]
    def sum_of_divisors(n):
        s=0
        for i in range(2,int(n**0.5)+1):
            if n%i==0 and i==n//i:
                s+=i
            elif n%i==0:
                s+=(i+n//i)
        return s+1  
    for i in range(1,28124):
        if sum_of_divisors(i)>i:
            abundant.append(i)
    t=int(input().strip())
    for _ in range(t):
        n=int(input().strip())
        if n<28124:
            i=0
            while n>=abundant[i]:
                if (n-abundant[i]) in abundant:
                    print("YES")
                    break
                i+=1
            else:
                print("NO")
        else:
            print("YES")