local fn IsPrime( n as NSUInteger ) as BOOL BOOL isPrime = YES NSUInteger i if n < 2 then exit fn = NO if n = 2 then exit fn = YES if n mod 2 == 0 then exit fn = NO for i = 3 to int(n^.5) step 2 if n mod i == 0 then exit fn = NO next end fn = isPrime local fn GCD( a as long, b as long ) as long long r if ( a == 0 ) then r = b else r = fn GCD( b mod a, a ) end fn = r local fn SumDiv( num as NSUInteger ) as NSUInteger NSUInteger div = 2, sum = 0, quot, result while (1) quot = num / div if ( div > quot ) then result = 0 : exit while if ( num mod div == 0 ) sum += div if ( div != quot ) then sum += quot end if div++ wend result = sum + 1 end fn = result local fn IsDuffinian( n as NSUInteger) as BOOL BOOL result = NO if ( fn IsPrime(n) == NO && fn GCD( fn SumDiv(n), n ) == 1 ) then exit fn = YES end fn = result local fn FindDuffinians long c = 0, n = 4 print "First 50 Duffinian numbers:" do if ( fn IsDuffinian(n) ) printf @"%4d \b", n c++ if ( c mod 10 == 0 ) then print end if n++ until ( c >= 50 ) c = 0 : n = 4 printf @"\n\nFirst 56 Duffinian triplets:" do if ( fn IsDuffinian(n) and fn IsDuffinian(n + 1) and fn IsDuffinian(n + 2) ) printf @" [%6ld %6ld %6ld] \b", n, n+1, n+2 c++ if ( c mod 4 == 0 ) then print end if n++ until ( c >= 56 ) end fn CFTimeInterval t t = fn CACurrentMediaTime fn FindDuffinians printf @"\nCompute time: %.3f ms",(fn CACurrentMediaTime-t)*1000 HandleEvents