Posts Tagged ‘csharp’

Project Euler Problem #142!

December 26th, 2009

Problem #142 say:

Find the smallest x + y + z with integers x > y > z > 0 such that x + y, x − y, x + z, x − z, y + z, y − z are all perfect squares.

Now one can brute force this by looping over x, y and z. That takes around 4 hours on a modern computer. However there are a few relationships we can take advantage of:

x+y=a
x-y=b
x+z=c
x-z=d
y+z=e
y-z=f

e=a-d
f=a-c
b=c-e

(x+z) = (x+y)-(x-z)
(y-z) = (x+y)-(x+z)
(x-y) = (x+z)-(y+z)

x=(a+b)/2
y=(e+f)/2
z=(c-d)/2

these relationships allow us to loop on a,c,d and get the rest of the numbers from those three, this makes the solution run in around 33 msecs. That’s around a ~436363% speedup! Solution provided in c#/mono.

using System;
using System.Collections;
using System.Diagnostics;

namespace ProjectEuler
{
    class Program
    {
        static object LockHandle = new object();
        static void Main(string[] args)
        {
            Stopwatch sw = new Stopwatch();
            long a2, b2, c2, d2, e2, f2;
            bool solved = false;
            sw.Start();

            for (long a = 10;!solved;a++)
            {
                a2 = a * a;
                for (long c = 5 + (0 & a); c < a && !solved; c += 2)
                {
                    c2 = c * c;
                    f2 = a2 - c2;
                    if (f2 < 1 || !IsSquare(f2))
                        continue;
                    for (long d = 2 + (1 & c); d < c; d += 2)
                    {
                        d2 = d * d;
                        e2 = a2 - d2;
                        if (e2 < 1 || !IsSquare(e2))
                            continue;
                        b2 = c2 - e2;
                        if (b2 > 0 && IsSquare(b2))
                        {
                            long x = (a2 + b2) / 2;
                            long y = (e2 + f2) / 2;
                            long z = (c2 - d2) / 2;
                            solved = true;
                            Console.WriteLine("x= " + x.ToString() +
                                 " y = " + y.ToString() + " z = " + z.ToString()
                                 + " sum = " + (z + y + x).ToString());
                            break;
                        }
                    }

                }
            }

            Console.WriteLine(sw.Elapsed);
            Console.ReadLine();
        }
        public static bool IsSquare(long n )
        {
            double root = Math.Sqrt(n);
            return (root % 1 == 0);
        }
    }
}

Tags: , , ,
Posted in Project Euler | Comments (0)

Project Euler Problem #206!

November 26th, 2009

Problem #206 says:

Find the unique positive integer whose square has the form 1_2_3_4_5_6_7_8_9_0,
where each “_” is a single digit.

The range of number to check can be narrowed down with a calculator and some common sense. Then its just a square and check procedure. Solution is in c# and requires the .Net 4.0 framework for its parallel extensions.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Parallel;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Numerics;
using System.Text.RegularExpressions;

namespace Net4Test
{
    class Program
    {
        static object LockHandle = new object();
        static long Answer = 0;
        static void Main(string[] args)
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            Regex Test = new Regex(
            "[1][0-9][2][0-9][3][0-9][4][0-9]" +
            "[5][0-9][6][0-9][7][0-9][8][0-9][9][0-9][0]");

            Parallel.For(1360000000, 1390000000, i =>
                {

                    BigInteger biI = i;

                    if (Test.IsMatch((biI * biI).ToString()))
                    {
                        Answer = i;
                    }
                });

            Console.WriteLine(Answer);
            sw.Stop();
            Console.WriteLine(sw.Elapsed);
            Console.ReadLine();
        }

    }
}

Tags: , ,
Posted in Project Euler | Comments (0)

Project Euler Problem #50!

November 5th, 2009

(updates on the site. pagerank just hit 3 on google. hopefully this will increase traffic a bit. and this post is the first post on the site done in windows 7 (took 6 hours for my pc to update :( )

Problem #50 says:

The prime 41, can be written as the sum of six consecutive primes:
41 = 2 + 3 + 5 + 7 + 11 + 13

This is the longest sum of consecutive primes that adds to a prime below one-hundred.
The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953.
Which prime, below one-million, can be written as the sum of the most consecutive primes?

The solution below is pretty much brute force. We generate the primes and then add them together and subtract from the total items in the list of primes. then we get the total of all of the primes minus the first item in the primes list and do it over again. there’s lots of room for improvements and when I get the time I’ll post them. but for now this is what I used to get the solution. Solution provided in c#/mono.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace ProjectEuler
{
    class Program
    {
        static void Main(string[] args)
        {
            Stopwatch sw = new Stopwatch();
            sw.Start();
            ArrayList primes = GeneratePrimes(1000000);
            primes.Remove(1);
            long result = 0;
            long maxlength = 0;
            Console.WriteLine("starting check");
            for (int start = 0;start<primes.Count;start++)
            {
                long temp = 0;
                for (int i = start; i < primes.Count; i++)
                {
                    temp = temp + (long)primes[i];
                }
                for (int k = primes.Count - 1; k > start; k--)
                {
                    if (temp < 1000000 && primes.Contains(temp) && k - start >= maxlength)
                    {
                        maxlength = k -start;
                        result = temp;
                        Console.WriteLine(result);
                        break;
                    }
                    temp = temp - (long)primes[k];
                    if (temp <= start)
                        break;
                }
            }
            Console.WriteLine(result);
            Console.WriteLine(maxlength);
            Console.WriteLine(sw.Elapsed);
            Console.ReadLine();
        }
        static ArrayList GeneratePrimes(long num)
        {
            ArrayList RetValue = new ArrayList();
            RetValue.Add((long)2);
            RetValue.Add((long)3);
            long Stepper = 5;
            long Check = 1;
            while (Stepper <= num)
            {
                foreach (long i in RetValue)
                {
                    if (Stepper % i == 0)
                    {
                        Check = 0;
                        break;
                    }
                    if (Math.Sqrt(Stepper) < i)
                    {
                        break;
                    }
                }
                if (Check == 1)
                {
                    //Console.WriteLine(((float)Stepper / (float)num) * 100);
                    RetValue.Add(Stepper);
                }
                Check = 1;
                Stepper++;
                Stepper++;
            }
            return RetValue;
        }
    }
}

Tags: , , , ,
Posted in Project Euler | Comments (0)