Skip to content

Instantly share code, notes, and snippets.

@JFFail
Created November 30, 2015 21:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JFFail/c401205715a23190728a to your computer and use it in GitHub Desktop.
Save JFFail/c401205715a23190728a to your computer and use it in GitHub Desktop.
Solution To Reddit Daily Programmer Easy #243 - Abundant and Deficient Numbers
using System;
using System.Collections;
//https://www.reddit.com/r/dailyprogrammer/comments/3uuhdk/20151130_challenge_243_easy_abundant_and/
//Reddit Daily Programmer Easy Challenge #243 - Abundant and Deficient Numbers
namespace AbunDef
{
class Program
{
//Function to find divisors.
public static ArrayList FindDivisors(int value)
{
ArrayList divisors = new ArrayList();
for(int i = 1; i <= value; i++)
{
if(value % i == 0)
{
divisors.Add(i);
}
}
return divisors;
}
//Function to find proper divisors.
public static ArrayList FindProperDivisors(int value)
{
ArrayList properDivisors = new ArrayList();
for(int i = 1; i < value; i++)
{
if(value % i == 0)
{
properDivisors.Add(i);
}
}
return properDivisors;
}
//Function to sum an ArrayList.
public static int SumArrayList(ArrayList divisorValues)
{
int sum = 0;
foreach(int number in divisorValues)
{
sum += number;
}
return sum;
}
static void Main(string[] args)
{
//Numbers to check.
int[] theNumbers = { 111, 112, 220, 69, 134, 85 };
//Loop through them all, checking each.
foreach (int theNumber in theNumbers)
{
//Check if it's an abundant number.
int properSum = SumArrayList(FindProperDivisors(theNumber));
if (properSum > theNumber)
{
int difference = properSum - theNumber;
Console.WriteLine("{0} abundant by {1}", theNumber, difference);
}
else
{
//Check if the number is deficient.
int divisorSum = SumArrayList(FindDivisors(theNumber));
if (divisorSum < (theNumber * 2))
{
Console.WriteLine("{0} deficient", theNumber);
}
else
{
Console.WriteLine("{0} ~~neither~~", theNumber);
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment