Saturday, December 29, 2018

Project Euler Problem 4

Largest palindrome product

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.



public class Program
{
    public static void Main(string[] args)
    {
        Console.WriteLine(Problem4.LargestPalindromeProduct(3));
    }
}
class Problem4
{
    internal static double LargestPalindromeProduct(int digits)
    {
        double upperLimit = Math.Pow(10, digits) - 1;
        double lowerLimit = Math.Pow(10, digits - 1) - 1;
        double palindrome = 0;
        for (double number = upperLimit; number > lowerLimit; number--)
        {
            palindrome = double.Parse(MakePalindrome(number.ToString()));
            for (double divisor = upperLimit; divisor > lowerLimit; divisor--)
            {
                if (palindrome > divisor * divisor)
                {
                    break;
                }
                if (palindrome % divisor == 0 && (palindrome / divisor) < upperLimit)
                {
                    return palindrome;
                }
            }
        }
        return palindrome;
    }
    private static string MakePalindrome(string number)
    {
        return number + new string(number.Reverse().ToArray());
    }
}
 

Project Euler Problem 3

Largest prime factor

The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?


public class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine(Problem3.LargestPrimeFactor(600851475143));   
        }
    }
class Problem3
    {                
        internal static long LargestPrimeFactor(long number)
        {
            long i = 2, factor=1;
            while(true)
            {
                if (number % i == 0)
                {
                    factor = number / i;
                    if (IsPrimeNumber(factor))
                    {
                        return factor;
                    }
                }                
                i++;
            }            
        }
        private static bool IsPrimeNumber(long number)
        {
            long divisor = 2;
            for (long i = number / 2; i > 1; i+=0)
            {
                if (number % i == 0return false;
                i = number / (++divisor);
            }
            return true;
        }
    }

Friday, December 28, 2018

Project Euler Problem 2


Even Fibonacci numbers


Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
Snippet
public class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine(Problem2.EvenFibonacciNumbers(4000000));
        }
    }
class Problem2
    {
        internal static long EvenFibonacciNumbers(long limit)
        {
            long curr = 1, next = 2, temp=0,res=2;            
            while(true)
            {
                temp = next;
                next += curr;
                if (next > limit) break;
                curr = temp;                
                res = (next % 2 == 0? res + next : res;                
            }
            return res;
        }
    }   
Steps
First two numbers will be 1 and 2
Take one temp variable to store next value
Next number will be sum of current and next
Assign value to current from temp

If number in the fibonacci series is divisible by 2, Add it to the result

Project Euler Problem 1


Multiples of 3 and 5

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.
Sinppet
public class Program
{
    public static void Main(string[] args)
    {
        Console.WriteLine(Problem1.GetMultiplesOf3or5(1000));
    }
}
class Problem1
{               
    internal static long GetMultiplesOf3or5(long limit)
    {           
        long sum = 0;
        for (long i = 3; i< limit; i++)
        {
            if ((i % 3 == 0) || (i % 5 == 0))
            {
                sum += i;
            }
        }
        return sum;
    }
}


Steps
  • Loop from 3 to 1000
  • Check if number is multiple of 3 or 5
  • If true, add it to the sum variable

Labels

How to take screenshot of a Webpage using C# Selenium

           ChromeOptions options = new ChromeOptions();             options.AddArgument("headless");//Comment if we want to see ...