Skip to content

Instantly share code, notes, and snippets.

@jamesbcook
Created February 17, 2014 08:51
Show Gist options
  • Save jamesbcook/9047027 to your computer and use it in GitHub Desktop.
Save jamesbcook/9047027 to your computer and use it in GitHub Desktop.
#!/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