Skip to content
Snippets Groups Projects
Commit 6c0dde75 authored by nimrod's avatar nimrod
Browse files

- Added Python, Lua and C versions.

- Added a gitignore file.
parent d419d1fb
No related branches found
No related tags found
No related merge requests found
a.out
*.pyc
prime.c 0 → 100644
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int isPrime(unsigned long x)
{
if (sqrt(x) - (unsigned long)sqrt(x) == 0)
return 0;
for (unsigned long i = 2; i < ceil(sqrt(x)); i++)
if (x % i == 0)
return 0;
return 1;
}
int main( int argc, char** argv)
{
unsigned long max = atol(argv[1]);
unsigned long *a = malloc(sizeof(unsigned long) * max);
unsigned long x = 2;
for (unsigned long i = 0; i < max; x++)
if (isPrime(x))
a[i++] = x;
for (unsigned long i = 0; i < max; i++)
printf("%lu\n", (unsigned long)a[i]);
return 0;
}
#!/usr/bin/env lua
function isPrime (x)
-- Here will be a for loop
if math.sqrt(x) % 1 == 0 then
return false
end
for i = 2, math.sqrt(x) do
if x % i == 0 then
return false
end
end
return true;
end
primes = {}
i = 0
while (#primes < tonumber(arg[1])) do
if isPrime(i) then
table.insert(primes, i)
end
i = i + 1
end
for key, value in pairs(primes) do
print(value)
end
prime.py 0 → 100755
#!/usr/bin/env python3
def is_prime (x):
from math import sqrt, ceil
if sqrt(x).is_integer():
return False
for i in range(2, ceil(sqrt(x))):
if x%i == 0:
return False
return True
if __name__ == '__main__':
from sys import argv
primes = []
i = 0
while len(primes) < int(argv[1]):
if is_prime(i):
primes.append(i)
i += 1
print(primes)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment