Advertisement
Guest User

Euler 21

a guest
Dec 15th, 2014
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.59 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Project_Euler_Problem_21
  7. {
  8.     class Program
  9.     {
  10.         static void Main()
  11.         {
  12.             // Accept only valid input!
  13.             int input = 0;
  14.             do
  15.             {
  16.                 Console.Write("Max Value: ");
  17.             } while (!int.TryParse(Console.ReadLine(), out input));
  18.  
  19.             Program MainProgram = new Program();
  20.  
  21.             // Execute the program!
  22.             int sum = MainProgram.FindAmicableSum(input);
  23.  
  24.             // Result.
  25.             Console.WriteLine("\nSum of Amicable Numbers: {0}", sum);
  26.             Console.ReadLine();
  27.         }
  28.  
  29.         public int FindAmicableSum(int maxValue)
  30.         {
  31.             // Heavy lifting all starts here.
  32.  
  33.             Number[] numberArray = new Number[maxValue + 1];
  34.  
  35.             // Create numbers.
  36.             for (int i = 0; i <= maxValue; i++)
  37.             {
  38.                 numberArray[i] = new Number(i);
  39.             }
  40.  
  41.             // Selects only amicable numbers.
  42.             return numberArray.Select(x => x.Amicable == true ? x.Value : 0).Sum();
  43.         }
  44.  
  45.     }
  46.  
  47.     public class Number
  48.     {
  49.         // Contains information about each individual number.
  50.  
  51.         public Number(int value)
  52.         {
  53.             // All information about the number is determined internally while being constructed.
  54.  
  55.             this.Value = value;
  56.             DivisorSum = FindDivisorSum(value);
  57.             TestAmicablity();
  58.         }
  59.        
  60.         public readonly int Value;
  61.         public int DivisorSum { get; private set; }
  62.         public bool Amicable { get; set; }
  63.  
  64.         private static int FindDivisorSum(int n)
  65.         {
  66.             // Just an intermediate step between finding the divisor and returning the sum.  Refactor all the things!
  67.  
  68.             List<int> divisorList = FindDivisors(n);
  69.             return divisorList.Sum();
  70.         }
  71.  
  72.         private static List<int> FindDivisors(int n)
  73.         {
  74.             // A linear search is performed.  Could be more efficient if I used O(n/m) complexity.
  75.  
  76.             List<int> divisorList = new List<int>();
  77.  
  78.             for (int i = 1; i < n; i++)
  79.             {
  80.                 if (n % i == 0)
  81.                 {
  82.                     divisorList.Add(i);
  83.                 }
  84.             }
  85.             return divisorList;
  86.         }
  87.  
  88.         private void TestAmicablity()
  89.         {
  90.             // Test amicability, pretty simple stuff.
  91.             Amicable = (Value == FindDivisorSum(DivisorSum) && Value != DivisorSum);
  92.         }
  93.     }
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement