diff --git a/C#/02.EvenFiboNumbers.cs b/C#/02.EvenFiboNumbers.cs new file mode 100644 index 0000000..759f27d --- /dev/null +++ b/C#/02.EvenFiboNumbers.cs @@ -0,0 +1,49 @@ +// Project Euler : Problem Number02 +using System; +namespace _2._Even_Fibonacci_numbers +{ + class EvenFiboNumbers + { + static int evenFibSum(int limit) + { + if (limit < 2) + return 0; + + // Initialize first two even + // prime numbers and their sum + long ef1 = 0, ef2 = 2; + long sum = ef1 + ef2; + + // calculating sum of even + // Fibonacci value + while (ef2 <= limit) + { + + // get next even value of + // Fibonacci sequence + long ef3 = 4 * ef2 + ef1; + + // If we go beyond limit, + // we break loop + if (ef3 > limit) + break; + + // Move to next even number + // and update sum + ef1 = ef2; + ef2 = ef3; + sum += ef2; + } + + return (int)sum; + } + + // Driver code + public static void Main() + { + int limit = 4000000; + Console.Write(evenFibSum(limit)); + + } + } +} diff --git a/C#/03.largest_prime_fac.cs b/C#/03.largest_prime_fac.cs new file mode 100644 index 0000000..e357784 --- /dev/null +++ b/C#/03.largest_prime_fac.cs @@ -0,0 +1,26 @@ +//Euler problem03 +using System; +namespace largest_prime_fac +{ + class largest_prime_fac + { + public static void Main() + { + long num = 600851475143; + int count = 3; + while (num > 1) + { + if (num % count == 0) + { + num /= count; + } + else + { + count += 2; + } + } + Console.WriteLine("The largest prime factor of the number {0} is {1} ", 600851475143, count); + Console.ReadKey(); + } + } +} diff --git a/C#/04.Largest_palindrome_product b/C#/04.Largest_palindrome_product new file mode 100644 index 0000000..5298e29 --- /dev/null +++ b/C#/04.Largest_palindrome_product @@ -0,0 +1,34 @@ +//Euler problem04 +using System; +namespace _Largest_palindrome_product +{ + class Largest_palindrome_product + { + public static void Main() + { + for (int i = 999; i >= 100; i--) + { + for (int j = 999; j >= 100; j--) + { + if (isPalindome(i * j) == true) + { + Console.WriteLine("Digit1: " + i); + Console.WriteLine("Digit2: " + j); + Console.WriteLine("Outcome: " + i * j); + Console.ReadLine(); + } + } + + } + Console.ReadLine(); + } + public static bool isPalindome(int num) + { + string sNum = num.ToString(); + for (int i = 0; i < sNum.Length / 2; i++) + if (sNum[i] != sNum[sNum.Length - 1 - i]) return false; + + return true; + } + } +} diff --git a/C#/05.Smallest_multiple.cs b/C#/05.Smallest_multiple.cs new file mode 100644 index 0000000..83c47ff --- /dev/null +++ b/C#/05.Smallest_multiple.cs @@ -0,0 +1,51 @@ +//Euler problem05 +using System; +using System.Collections.Generic; +using System.Linq; + +namespace _Smallest_multiple +{ + class Smallest_multiple + { + public static void Main() + { + Console.WriteLine(Calc(20)); + } + static List Factors(int n) + { + List list = new List(); + int i = 2; + while (i <= n) + { + if (n % i == 0) + { + list.Add(i); + n /= i; + } + else + i++; + } + return list; + } + + static int Calc(int n) + { + var factors = Enumerable.Range(2, n - 1).Select(j => Factors(j)).ToList(); + + int i = 2; + int res = 1; + while (i <= n) + { + if (factors.Count(l => l.Contains(i)) > 1) + { + factors.ForEach(l => l.Remove(i)); + res *= i; + } + else + i++; + } + + return res * factors.SelectMany(x => x).Aggregate((s, j) => s *= j); + } + } +} diff --git a/C#/06.Sum_square_difference b/C#/06.Sum_square_difference new file mode 100644 index 0000000..c9f0f64 --- /dev/null +++ b/C#/06.Sum_square_difference @@ -0,0 +1,17 @@ +//Euler problem06 +using System; + +public class Sum_square_difference +{ + public static void Main() + { + int n=10; + //sum of square first 100numbers + //1^2+2^2+3^2.... + long sum1=(n*(n+1)*(2*n+1))/6; + //sum of first 100 numbers square + //(1+2+3...)^2 + long sum2=((n*(n+1))/2)*((n*(n+1))/2); + Console.WriteLine(sum2-sum1); + } +} diff --git a/C#/07.Prime_10001st.cs b/C#/07.Prime_10001st.cs new file mode 100644 index 0000000..96ed602 --- /dev/null +++ b/C#/07.Prime_10001st.cs @@ -0,0 +1,27 @@ +//Euler problem07 +using System; +using System.Collections.Generic; +using System.Linq; + +namespace _Prime_10001st +{ + class Prime_10001st + { + public static void Main() + { + List primeNumbers = new List() { 2 }; + for (long i = 3; i < long.MaxValue; i += 2) + { + if (!primeNumbers.Any(p => (i % p) == 0)) + { + primeNumbers.Add(i); + if (primeNumbers.Count == 10001) + { + Console.WriteLine(i); + break; + } + } + } + } + } +}