Saturday, July 27, 2013

Project Euler Solved using C# : Smallest multiple


2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

 The Solution to this problem is very simple simply what one has to do is logically think what the question demands, The problem statement wants you to find the largest common factor to all the given numbers, so what you have to do is to find the largest common factor to all the numbers from1 to 20.

The program to solve this problem in C# is below, call this class in the "static main()" function and get the required solution. Answer to this problem is :232792560

 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Text;  
 namespace ProjectEuler  
 {  
   class LargestCommonFactor  
   {  
     private int _upperlimit = 20;  
     private List<int> _primenumbers = new List<int>();  
     private List<int> _LCMcandidates = new List<int>();  
     public Dictionary<int, int> _primepowerpair = new Dictionary<int, int>();  
     public LargestCommonFactor() 
     {  
       getPrimeNumbers();  
       getAllCandidates();  
       getComparisonArray();  
     }  
     private void getPrimeNumbers()  
     {  
       for (int i = 2; i < _upperlimit; i++)  
       {  
         if (checkIfPrime(i)) { _primenumbers.Add(i); }  
       }  
     }  
     private Boolean checkIfPrime(int suspectprime)  
     {  
       for (int j = 2; j < suspectprime; j++)  
       {  
         if (suspectprime % j == 0) { return false; }  
       }  
       return true;  
     }  
     private void getAllCandidates()  
     {  
       for (int i = 1; i <= _upperlimit; i++)  
       {  
         _LCMcandidates.Add(i);  
       }  
     }  
     private void getComparisonArray()  
     {  
       int[,] LCMArray = new int[_primenumbers.Count + 1, _LCMcandidates.Count + 1];  
       int[] LCMCandidates = _LCMcandidates.ToArray();  
       int[] primenumbers = _primenumbers.ToArray();  
       for (int i = 1; i < _LCMcandidates.Count + 1; i++)  
       {  
         LCMArray[0, i] = LCMCandidates[i - 1];  
       }  
       for (int i = 1; i < _primenumbers.Count + 1; i++)  
       {  
         LCMArray[i, 0] = primenumbers[i - 1];  
       }  
       fillComparisonArray(LCMArray);  
       printComparisonArray(LCMArray);  
       printLargestCommonFactor(LCMArray);  
     }  
     private void fillComparisonArray(int[,] array)  
     {  
       for (int i = 1; i < _primenumbers.Count + 1; i++)  
       {  
         for (int j = 1; j < _LCMcandidates.Count + 1; j++)  
         {  
           array[i, j] = getPowerofPrime(array[0, j], array[i, 0]);  
         }  
       }  
     }  
     private int getPowerofPrime(int number, int prime)  
     {  
       int temp = number;  
       if (number >= prime)  
       {  
         int counter = 0;  
         while (temp >= prime)  
         {  
           if (temp % prime == 0)  
           {  
             temp = temp / prime;  
             counter++;  
           }  
           else return 0;  
         }  
         return counter;  
       }  
       return 0;  
     }  
     private void printComparisonArray(int[,] array)  
     {  
       for (int i = 0; i < 9; i++)  
       {  
         for (int j = 0; j < 21; j++)  
         {  
           Console.Write(array[i, j] + " ");  
         }  
         Console.WriteLine();  
       }  
     }  
     private void printLargestCommonFactor(int[,] array)  
     {  
       for (int i = 1; i < _primenumbers.Count + 1; i++)  
       {  
         List<int> temp = new List<int>();  
         for (int j = 1; j < _LCMcandidates.Count + 1; j++)  
         {  
           temp.Add(array[i, j]);  
         }  
         extractMaxPowerofPrime(array[i, 0], temp);  
       }  
       printDictonary();  
     }  
     private void extractMaxPowerofPrime(int prime, List<int> powerset)  
     {  
       _primepowerpair.Add(prime, powerset.Max());  
     }  
     private void printDictonary()  
     {  
       double lcm = 1;  
       foreach (KeyValuePair<int, int> d in _primepowerpair)  
       {  
         Console.WriteLine("prime number is is: " + d.Key.ToString() + " power is: " + d.Value);  
         lcm = lcm * Math.Pow((double)Convert.ToInt32(d.Key.ToString()), (double)d.Value);  
       }  
       Console.WriteLine(lcm);  
     }  
   }  
 }  

No comments:

Post a Comment