Permalink
Browse files

Minor optimizations

  • Loading branch information...
1 parent 51bbf5c commit 4d448cd6d6865d42fa9945ae053d9fe082594c55 @joshuaclayton committed Aug 29, 2010
Showing with 6 additions and 8 deletions.
  1. +6 −8 ext/sieve/sieve.c
View
@@ -18,16 +18,15 @@ static VALUE sieve(const VALUE self) {
rb_raise(rb_eNoMemError, "Can't allocate enough memory.");
}
- long i;
- for(i = 0; i < number; i++) {
- numbers[i] = i;
- }
numbers[0] = numbers[1] = -1;
+ long i;
+ for(i = 2; i < number; i++) { numbers[i] = i; }
long current_square;
for(i = 0; i < number; i++) {
+ if(numbers[i] == -1) { continue; }
+
current_square = powl(i, 2);
- if(numbers[i] == -1) { continue; }
if(current_square > number) { break; }
long n;
@@ -38,9 +37,8 @@ static VALUE sieve(const VALUE self) {
VALUE primes_array = rb_ary_new();
for(i = 0; i < number; i++) {
- if(numbers[i] != -1) {
- rb_ary_push(primes_array, LONG2FIX(numbers[i]));
- }
+ if(numbers[i] == -1) { continue; }
+ rb_ary_push(primes_array, LONG2FIX(numbers[i]));
}
free(numbers);

0 comments on commit 4d448cd

Please sign in to comment.