Search This Blog

Wednesday, December 29, 2010

Euler problem 41

Last one for today… this isn’t fast code but works…

A bit of a speed up implemented by only checking those which are of length equal to their max character… so there’s not point checking 17 as it’s 2 in length.. so max permitted would be 12… however 132 is fine as it’s 3 in length and has a 3 in it!

_______________________

static void Main(string[] args)
        {
            long max=0;

            for (long i = 1; i < 987654321; i++)
            {
                int len = i.ToString().Length;

               //find max char
                int maxchar = 0;
                foreach (char c in i.ToString())
                {
                    if (c - 48 > maxchar)
                    {
                        maxchar = c - 48;
                    }
                }

               //only check if this could be pandigital - by checking max char and len
                //eg1 17 can't be pandigital as only 2 chars long (eg2 12 =OK, 13=not)

                string s = i.ToString();
                if (maxchar == len && isprime(i))
                {
                 if (s.Split('1').Length==2 && s.Split('2').Length==2 )
                 {
                    if (i.ToString().Length==2 && i > max) {max=i;}
                   
                     if  (s.Split('3').Length==2)
                     {
                         if (i.ToString().Length==3 && i > max) {max=i;}
                         if (s.Split('4').Length==2)
                         {
                             if (i.ToString().Length==4 && i > max) {max=i;}
                              if (s.Split('5').Length==2)
                              {
                                  if (i.ToString().Length==5 && i > max) {max=i;}
                                  if (s.Split('6').Length==2)
                                  {
                                      if (i.ToString().Length==6 && i > max) {max=i;}
                                       if (s.Split('7').Length==2)
                                       {
                                           if (i.ToString().Length==7 && i > max) {max=i;}
                                           if (s.Split('8').Length == 2)
                                           {
                                               if (i.ToString().Length == 8 && i > max) { max = i; }
                                               if (s.Split('9').Length == 2)
                                               {
                                                   if (i.ToString().Length == 9 && i > max) { max = i; }
                                                   break;
                                               }
                                           }
                                       }
                                  }
                              }
                         }
                     }
                 }
                }
            }
            Console.WriteLine(max);
            Console.ReadLine();

        }

 

static bool isprime(long n)
       {
           if (n == 2 || n == 3 || n == 5) { return true; }

          if (n%2==0) {return false;}
          if (n%3 == 0) { return false;}
          if (n%5 == 0) { return false;}

          for (int t = 7; t < Math.Sqrt(n); t++)
          {
              if (n % t== 0)
              {
                  return false;
              }
          }

          return true;

       }

Euler Problem 40

Brute force attack!!!

Keep making the string longer until you hit 1 million chars long.. then do the calc they want.. rem first place is s[0]…. and you’re sorted!

____

static void Main(string[] args)
{
    string s = "";
    int i=0;
    while (s.Length<1000001)
   {
       i++;
        s += i.ToString();
    }

    int sum = (Convert.ToInt32(s[0])-48) * (Convert.ToInt32(s[9])-48) * (Convert.ToInt32(s[99])-48) * (Convert.ToInt32(s[999])-48) * (Convert.ToInt32(s[9999])-48) * (Convert.ToInt32(s[99999])-48) * (Convert.ToInt32(s[999999])-48);
   
 
    Console.WriteLine("Answer>>"+sum);
    Console.ReadLine();

}

Euler Problem 39

An easier one… some geometry and Pythog theroem..

I just made a big array, used that as a counter for each solution.. then scanned it to find the answer.

Just remember that the counter is not for c(the hyp) but for the perimeter length (p=a+b+c)…. !

____________

static void Main(string[] args)
       {
           int[] cc = new int[1001];  //somewhere to store the counts

           for (int a = 1; a < 999; a++)
           {
               for (int b = 1; b < 999; b++)
               {
                   double c = Math.Sqrt(a * a + b * b);

                   if (c%1==0 && (a+b+c <= 1000))  //must be int length sides and per<=1000
                   {
                      
                       cc[Convert.ToInt32(a+b+c)]++;
                   }
               }
           }
           //now find largest value
           int maxp = 0;
           int maxn = 0;
           for (int i = 0; i < 1000; i++)
           {
               if (cc[i] > maxn)
               {
                   maxn = cc[i];
                   maxp = i;
               }
           }

           Console.WriteLine("Answer>>"+maxp + " count of "+maxn);
           Console.ReadLine();

       }

Euler Problem 38

So here you need to do some pandigital checks again like problem 32…

We know the answer must be bigger than that given in the example.. as I tried putting their example answer in to see if it was the answer they were after.. but no…! Anyway this means the final value is somewhere between 91873654 and 987654321… so that’s a start.

I was thinking the number of parts to split up the 9 digit number into may be 2,3,4.. as in 123,456789 would be 2 parts.. but to be a correct value then part 1 x 2 should equal part 2… (not here obviously as 123 * 2 = 246)…

My plan was to try this with 2 parts.. and if it didn’t work then look at 3 parts.. and so on.. but I ran it with 2 parts and got the answer so didn’t need to go any further than this!!

Not the smartest, or most elegant solution… but it did the job..

____________________

static void Main(string[] args)
{
//must be bigger than 918273654 as it's mentioned in question
//and I tested it as the answer and it wasn't..
for (int i = 987654321; i > 918273645; i--)
{
string s = i.ToString();

//contains 1-9 once - pandigital 9
if (
s.Split('1').Length == 2 &&
s.Split('2').Length == 2 &&
s.Split('3').Length == 2 &&
s.Split('4').Length == 2 &&
s.Split('5').Length == 2 &&
s.Split('6').Length == 2 &&
s.Split('7').Length == 2 &&
s.Split('8').Length == 2 &&
s.Split('9').Length == 2)
{

//check case of 2 parts
for (int n=1;n<9;n++)
{
int part1 = Convert.ToInt32(i.ToString().Substring(0,n));
int part2= Convert.ToInt32(i.ToString().Substring(n,i.ToString().Length-n));

if (part2==part1*2)
{
Console.WriteLine(i);
goto foo;
}

}

}//end of pandigital check
}


foo:
Console.WriteLine("Done!");
Console.ReadLine();

}

Euler Problem 37

http://projecteuler.net/index.php?section=problems&id=37

The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3.

Find the sum of the only eleven primes that are both truncatable from left to right and right to left.

NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.

______

so the first thing I did wrong was to consider 1 to be a prime.. once I’d sorted that out I got this list.. which is 11 primes…  so those must be what they’re after!

23
37
53
73
313
317
373
797
3137
3797
739397

_________________

 

static void Main(string[] args)
       {
           long sum = 0;
           int found = 0;
           long i = 11;
           while (found<11)
           {
              int c=0;
              i++;
              int l = i.ToString().Length;

              for (int x = 0; x < l; x++)
              {

                  long t1 = Convert.ToInt64(i.ToString().Substring(x, l - x));
                  long t2 = Convert.ToInt64(i.ToString().Substring(0, x+1));

                  if (isprime(t1) && isprime(t2) && t1 != 1 && t2 != 1 )
                  {
                      c++;
                  }
                  else
                  {
                      c = 0;
                  }

              }
              
               if (c == i.ToString().Length)
                   {
                       sum += i;
                       found++;
                       Console.WriteLine(i);
                   }

           }

           Console.WriteLine("Answer>>>"+sum);
           Console.ReadLine();

       }

static bool isprime(long n)
       {
           if (n == 2 || n == 3 || n == 5) { return true; }

          if (n%2==0) {return false;}
          if (n%3 == 0) { return false;}
          if (n%5 == 0) { return false;}

          for (int t = 7; t < Math.Sqrt(n); t++)
          {
              if (n % t== 0)
              {
                  return false;
              }
          }

          return true;

       }

Euler Problem 36

Just count from 1 to n, find equivalent binary, check both base 10 and base 2 values are palindromic.. job done!

Interestingly in .NET to turn binary into base 10 then just do this!

Convert.ToInt32("1001",2)    

1001 ==>> 9

________________

 

static void Main(string[] args)
       {
           int counter = 0;
           int sum = 0;

           for (int i = 1; i < 1000000; i++)
           {
               string binary = IntToString(i, new char[] { '0', '1' });

               if (palindromic(i.ToString()) && palindromic(binary))
               {
                   sum += i;
               }

           }

           Console.WriteLine(sum);
           Console.ReadLine();

       }


       static bool palindromic(string s)
       {
           string rev="";
           for (int i = s.Length;i>0;i--)
           {
               rev += s.Substring(i-1,1);
           }

           if (s == rev)
           {
               return true;
           }

           return false;

       }

Tuesday, December 28, 2010

Euler Problem 35

Fairly straight forward this one…

________________

static void Main(string[] args)
        {
            int counter = 0;

            for (long i = 2; i < 1000000; i++)
            {
                string s = i.ToString();
                int l= s.ToString().Length;

                int c = 0;

                for (int x = 0; x <l; x++)
                {
                    string sr = s.Substring(x, l - x) + s.Substring(0,x);
                    if (!isprime(Convert.ToInt32(sr)))
                    {
                        break;
                    }
                    else
                    {
                        c++;
                    }


                }

                if (c==l) {
                    counter++;}


            }

            Console.WriteLine(counter);
            Console.ReadLine();

        }

 

static bool isprime(int n)
       {
           if (n == 2 || n == 3 || n == 5) { return true; }

          if (n%2==0) {return false;}
          if (n%3 == 0) { return false;}
          if (n%5 == 0) { return false;}

          for (int t = 7; t < Math.Sqrt(n); t++)
          {
              if (n % t== 0)
              {
                  return false;
              }
          }

          return true;

       }