Last active
August 29, 2015 13:56
-
-
Save alaafqandil/8870737 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace LargestPrimeFactor | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
//http://projecteuler.net/problem=3 | |
var before = DateTime.Now; | |
double UserInput = 600851475143;// 600, 851, 475, 143 --> four places | |
int initialValue = 10000; | |
IEnumerable<int> PrimeNumbersList = GetListOfPrimeNumbers(initialValue); | |
Console.WriteLine("Input Number is: " + UserInput); | |
Console.WriteLine( ); | |
double LargestPrime = UserInput; | |
foreach (int item in PrimeNumbersList) | |
{ | |
//System.IO.File.AppendAllText("a.txt" ,Environment.NewLine + item.ToString() ); | |
if (LargestPrime % item == 0) | |
{ | |
Console.WriteLine(item + ", " ); | |
var res = LargestPrime / item; | |
if (LargestPrime <= item) | |
{ | |
Console.WriteLine(item); | |
break; | |
} | |
} | |
} | |
var after = DateTime.Now; | |
Console.WriteLine(); | |
Console.WriteLine(after - before); | |
} | |
private static IEnumerable<int> GetListOfPrimeNumbers(int num) | |
{ | |
IEnumerable<int> req = Enumerable.Range(0, (int) | |
Math.Floor(2.52 * Math.Sqrt(num) / Math.Log(num))).Aggregate( | |
Enumerable.Range(2, (int)num - 1).ToList(), | |
(result, index) => | |
{ | |
var bp = result[index]; | |
var sqr = bp * bp; | |
result.RemoveAll(i => i >= sqr && i % bp == 0); | |
return result; | |
} | |
); | |
return req; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment