Created
February 17, 2014 08:51
-
-
Save jamesbcook/9047027 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'inline' | |
class Primes | |
inline do |builder| | |
builder.c ' | |
void prime(unsigned long amount) { | |
int *list; | |
unsigned long i,x,d; | |
list = malloc(sizeof(int)*(amount)); | |
for (i=2; i <= amount; i++){ | |
list[i] = 1; | |
} | |
for(x=2; x<=amount; x++) { | |
if (list[x]) { | |
for (d = x; d * x < amount ; d++) { | |
list[d * x] = 0; | |
} | |
} | |
} | |
for (i=2; i < amount; i++){ | |
if (list[i]){ | |
printf("%d\n",i); | |
} | |
} | |
}' | |
end | |
end | |
def help | |
puts "#{$0} <primes_up_to>" | |
end | |
begin | |
unless ARGV.length == 1 | |
help | |
exit(1) | |
end | |
prime = Primes.new | |
prime.prime(ARGV[0].to_i) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment