include "NSlog.incl" CFStringRef local fn PrimeFactors( num as UInt64 ) UInt64 i, count = 0, n = num if ( n < 0 ) then n = -n while ( n % 2 == 0 ) mda(count) = 2 : count++ : n = n/2 wend for i = 3 to sqr(n) + 1 step 2 while ( n % i == 0 ) mda(count) = i : count++ : n = n/i wend next if ( n > 2 ) then mda(count) = n if ( num < 0 )then mda(0) = -mda_integer(0) CFStringRef factorsStr = fn ArrayComponentsJoinedByString( fn MDAArray(0), @" * " ) : mda_kill return fn StringWithFormat( @"%19llu factors: %@", num, factorsStr ) end fn = NULL CFTimeInterval t : t = fn CACurrentMediaTime NSLog( @"%@", fn PrimeFactors( 3 ) ) NSLog( @"%@", fn PrimeFactors( 7 ) ) NSLog( @"%@", fn PrimeFactors( 31 ) ) NSLog( @"%@", fn PrimeFactors( 127 ) ) NSLog( @"%@", fn PrimeFactors( 2047 ) ) NSLog( @"%@", fn PrimeFactors( 8191 ) ) NSLog( @"%@", fn PrimeFactors( 131071 ) ) NSLog( @"%@", fn PrimeFactors( 524287 ) ) NSLog( @"%@", fn PrimeFactors( 8388607 ) ) NSLog( @"%@", fn PrimeFactors( 536870911 ) ) NSLog( @"%@", fn PrimeFactors( 2147483647 ) ) NSLog( @"%@", fn PrimeFactors( 137438953471 ) ) NSLog( @"%@", fn PrimeFactors( 2199023255551 ) ) NSLog( @"%@", fn PrimeFactors( 8796093022207 ) ) NSLog( @"%@", fn PrimeFactors( 140737488355327 ) ) NSLog( @"%@", fn PrimeFactors( 9007199254740991 ) ) NSLog( @"%@", fn PrimeFactors( 576460752303423487 ) ) NSLog( @"\nTime to compute all these factors: %.3f ms",(fn CACurrentMediaTime-t)*1000 ) HandleEvents