Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local function findPrimes(range)
- local lim, num, j;
- local is_prime = {};
- is_prime[2] = true;
- is_prime[3] = true;
- for i = 5, range do
- is_prime[i] = false;
- end
- local lim = math.ceil(math.sqrt(range));
- for x = 1, lim do
- for y = 1, lim do
- num = (4 * x^2 + y^2);
- if(num <= range and (num % 12 == 1 or num % 12 == 5)) then
- is_prime[num] = true;
- end
- num = (3 * x^2 + y^2);
- if(num <= range and (num % 12 == 7)) then
- is_prime[num] = true;
- end
- if(x > y) then
- num = (3 * x^2 - y^2);
- if(num <= range and (num % 12 == 11)) then
- is_prime[num] = true;
- end
- end
- end
- end
- for i = 5, lim do
- if(is_prime[i]) then
- j = i^2;
- while(j <= range) do
- is_prime[j] = false;
- j = j + i;
- end
- end
- end
- local primes = {};
- for i = 2, range do
- if(is_prime[i]) then
- table.insert(primes, i);
- end
- end
- table.sort(primes);
- return primes;
- end
Advertisement
Add Comment
Please, Sign In to add comment