Project Euler #23: Non-abundant sums

  • + 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));
    }
    

    }