C# Implementation of Problem 5

View source code here on GitHub!

class p0005
: Euler.IEuler
object Answer ()
 1/*
 2Project Euler Problem 5
 3
 4Problem:
 5
 62520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
 7
 8What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
 9*/
10using System;
11
12namespace Euler
13{
14    public class p0005 : IEuler
15    {
16        public object Answer()
17        {
18            int answer = 1;
19            byte[] factorTracker = new byte[20], localFactorTracker = new byte[20];
20            for (byte i = 2; i < 21; i++)
21            {
22                foreach (byte p in Prime.PrimeFactors(i))
23                    localFactorTracker[p]++;
24                for (byte j = 2; j < 20; j++)
25                {
26                    factorTracker[j] = Math.Max(factorTracker[j], localFactorTracker[j]);
27                    localFactorTracker[j] = 0;
28                }
29            }
30            for (byte i = 2; i < 20; i++)
31                for (byte j = 0; j < factorTracker[i]; j++)
32                    answer *= i;
33            return answer;
34        }
35    }
36}

Tags: csharp-iterator, divisibility, factorization, prime-number