Permalink
Browse files

[euler/prob047] add version using NativeCall

  • Loading branch information...
1 parent 1621587 commit 4ed4ade5db47fda30a55e9f57b1108fd94662da6 @gerdr gerdr committed Dec 2, 2012
Showing with 48 additions and 0 deletions.
  1. +32 −0 euler/prob047-gerdr.c
  2. +16 −0 euler/prob047-gerdr.pl
View
@@ -0,0 +1,32 @@
+#include <assert.h>
+
+// big enough for N = 4
+#define CACHE_SIZE (1 << 18)
+
+static int cache[CACHE_SIZE];
+
+int factors(int n)
+{
+ if(n < 2)
+ return 0;
+
+ assert(n < CACHE_SIZE);
+
+ if(cache[n])
+ return cache[n];
+
+ for(int p = 2; p * p <= n; ++p)
+ {
+ if(n % p == 0)
+ {
+
+ int r = n / p;
+ while(r % p == 0)
+ r /= p;
+
+ return (cache[n] = 1 + factors(r));
+ }
+ }
+
+ return (cache[n] = 1);
+}
View
@@ -0,0 +1,16 @@
+use v6;
+use NativeCall;
+
+sub factors(int $n) returns int is native('./prob047-gerdr') { * }
+
+sub MAIN(Int $N = 4) {
+ my int $n = 2;
+ my int $i = 0;
+
+ while $i != $N {
+ $i = factors($n) == $N ?? $i + 1 !! 0;
+ $n = $n + 1;
+ }
+
+ say $n - $N;
+}

0 comments on commit 4ed4ade

Please sign in to comment.