Puzzle 8-13-2008
Steven Schveighoffer
schveiguy at yahoo.com
Wed Aug 13 13:10:26 PDT 2008
"Wyverex" wrote
>> 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
1472 is not prime.
Hint for doing prime stuff, don't use floating point types :)
Also, the second to last line, taking the max of l and m is questionable.
-Steve
More information about the Digitalmars-d-learn
mailing list