Commit 68f931f3 authored by Samarth Joshi's avatar Samarth Joshi

uploading Sieve solution and testcases

parent 0a314571
import functools as ft import functools as ft
import sys
def Fib(n): def Fib(n):
## write your code here primes = range(2, n+1)
\ No newline at end of file
space = filter(lambda x: x*x<=n, primes)
multiples = map(lambda x: set(range(x*x, n+1, x)), space)
uniq = ft.reduce(lambda x,y: x.union(y), multiples)
actual_primes = filter(lambda x: x not in uniq, primes)
output = ft.reduce(lambda x,y: str(x)+" "+str(y), actual_primes)
print(output + " ")
Fib(int(sys.argv[1]))
\ No newline at end of file
import sys
def SieveOfEratosthenes(n):
# Create a boolean array "prime[0..n]" and initialize
# all entries it as true. A value in prime[i] will
# finally be false if i is Not a prime, else true.
prime = [True] * (n+1)
p = 2
while (p * p <= n):
# If prime[p] is not changed, then it is a prime
if (prime[p] == True):
# Update all multiples of p
for i in range(p * 2, n + 1, p):
prime[i] = False
p += 1
prime[0]= False
prime[1]= False
# Print all prime numbers
for p in range(n + 1):
if prime[p]:
print(str(p) + " ", end=''), #Use print(p) for python 3
print()
SieveOfEratosthenes(int(sys.argv[1]))
\ No newline at end of file
#!/bin/bash
for i in {10..1000}
do
python3 q1.py $i > mine
python3 sieve.py $i > sieves
diff -Z mine sieves > result
if [[ -s result ]];
then
echo failed $i
else
echo passed $i
fi
rm -f result
rm -f mine
rm -f sieves
done
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment