Thursday, August 23, 2012

Project Euler – Problem 4

This problem allows us to use recursion and some type conversions.

using System;

namespace Problem4
{
    class MainClass
    {
        // Function to check if a number is a palindrome
        static bool isPalindrome(string numStr)
        {
            int numLen;
            numLen=numStr.Length;

            if (numLen<=1){
                return true;
            } else {
                return (numStr[0]==numStr[numLen-1] && isPalindrome(numStr.Substring(1,numLen-2)));
            }

        }

        // Main function
        public static void Main(string[] args)
        {
            int n1,n2;
            int maxVal=0;

            for (n1=100;n1<=999;n1++)
            {
                for (n2=n1;n2<=999;n2++)
                {
                    if (isPalindrome(Convert.ToString(n1*n2))){
                        if (n1*n2>maxVal){
                            maxVal=n1*n2;
                        }
                    }
                }
            }
            Console.WriteLine("The answer is {0}",maxVal);
        }
    }
}

Sunday, August 19, 2012

Project Euler – Problem 3

Problem 3 showcases the use of functions, a while loop, and a call to the Math library.


using System;

class Program
{
    // Function that checks whether a number is prime
    static bool isPrime(long n)
    {
        int i;

        for (i=2;i<=Math.Sqrt(n);i++)
        {
            if (n%i==0) return false;
        }
        return true;

    }

    // Function that returns the next prime after a given number
    static long nextPrime(long n)
    {
        n++;

        while (!isPrime (n))
        {
            n++;
        }

        return n;

    }

    // Function that returns the largest prime factor of a number
    static long LargestPrimeFactor(long n)
    {
        long i=2;

        while (!isPrime (n))
        {
            if (n%i==0){
                n = n/i;
            } else {
                i=nextPrime(i);
            }
        }

        return n;
    }

    static void Main(string[] args)
    {
        Console.WriteLine("{0}", LargestPrimeFactor(600851475143));

        Console.ReadKey();
    }
}

Friday, August 17, 2012

Project Euler – Problem 2

 
using System;

namespace Problem2
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            int n1,n2,n3,total;

            n1=1;
            n2=2;
            n3=n1+n2;
            total=2;

            while (n3<4000000)
            {
                if (n3%2==0) total+=n3;

                n1=n2;
                n2=n3;
                n3=n1+n2;
            }

            Console.WriteLine ("Sum of even valued terms is {0}",total);
        }
    }
}
 
 

Project Euler – Problem 1

 
Project Euler Problem 1 in C#. Basic use of the for loop, if statement, and the Console.WriteLine function.

using System;

namespace Problem1
{
    class MainClass
    {
        public static void Main (string[] args)
        {
            int i;
            int total=0;

            for (i=1;i<1000;i++){
                if (i%5==0 || i%3==0) total+=i;
            }

            Console.WriteLine("The result is {0}",total);

        }
    }
}