We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
//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));
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
An unexpected error occurred. Please try reloading the page. If problem persists, please contact support@hackerrank.com
Project Euler #23: Non-abundant sums
You are viewing a single comment's thread. Return to all 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; }
}
class Solution { public static void Main(string[] args) { int t = Convert.ToInt32(Console.ReadLine().Trim()); List results = new List();
}