Puzzle 8-13-2008

Wyverex wyverex.cypher at gmail.com
Wed Aug 13 12:57:02 PDT 2008


Wyverex wrote:
> I've yet to try these but they look more challenging!
> 
> 
> 1)  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.

import tango.io.Stdout;

void main()
{
   int[] list;
   for(int i = 1; i < 1000; i++)
     if( (i % 5 == 0) || (i % 3 == 0) ) list ~= i;

   long sum;
   foreach(i;list) sum += i;

   Stdout(sum).newline;
}

output: 233168

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

import tango.io.Stdout;

T max( T )( T a, T b) { return ( a > b ? a : b ); }

void main()
{
   real i = 600851475143;
   real m = 0;

   real l = 2;
   while(l*l <= i)
   {
     if(i%l == 0)
     {
      m = max(m,l);
      Stdout(l)(" ");
      i /= l;
     }
     ++l;
   }
   Stdout(l).newline;
   m = max(m,l);
   Stdout("Max: ")(cast(long)m).newline;
}

output:
71.00 839.00 1471.00 1472.00
Max:1472

> 
> 3)  A Pythagorean triplet is a set of three natural numbers, a  b  c, 
> for which,
> a2 + b2 = c2
> 
> For example, 32 + 42 = 9 + 16 = 25 = 52.
> 
> There exists exactly one Pythagorean triplet for which a + b + c = 1000.
> Find the product abc.



import tango.io.Stdout;

void main()
{
   long a, b, c;
   //  0 < a < b < c

   for(c = 1000; c > 0; c--)
     for(b = 1000-c; b > 0; b--)
     {
       a = 1000 - c - b;
       if( a == 0 || a > b || b > c ) continue;
       if((a*a) + (b*b) == (c*c))
         Stdout.formatln("A:{} B:{} C:{}", a,b,c);
     }
}

output:
A:200 B:375 C:425



More information about the Digitalmars-d-learn mailing list