Saturday, July 27, 2013

Project Euler Solved using C# : 10001st prime


By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
What is the 10001st prime number?

what you have to do is traverse and check if the number is prime. The most important concept to remember is
  • 2 is the only EVEN prime number
  • 3 onwards all prime numbers are ODD
  • a number is considered to be prime if it is Divisible by any number Less Than or Equal To SQUAREROOT of (Suspected Number).
Answer to the Problem is : 104743

 
 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Text;  
 namespace ProjectEuler  
 {  
   class PrimeAtPlace  
   {  
   

Project Euler Solved using C# : Sum square difference


The sum of the squares of the first ten natural numbers is,
12 + 22 + ... + 102 = 385
The square of the sum of the first ten natural numbers is,
(1 + 2 + ... + 10)2 = 552 = 3025
Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.
Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

Remember Sigma(n) = (n*(n+1))/2
and Sigma(n^2) = (n*(n+1)*(2n+1))/6 

Equating them w.r.t to given problem = Sigma(n)^2 - Sigma(n^2) is

(n * ((n * n) - 1) * ((3 * n) + 2)) / 12

Answer to the Problem is :  25164150
 using System;  
 namespace ProjectEuler  
 {  
   class SquareAndSum  
   { 

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()