Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View inline_prime.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#!/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
Something went wrong with that request. Please try again.