mirror of
https://github.com/flancian/garden.git
synced 2026-08-07 15:06:19 +00:00
autopushed
This commit is contained in:
+8
-8
@@ -8,7 +8,6 @@ import math
|
|||||||
import sys
|
import sys
|
||||||
|
|
||||||
# I'm not proud (I am a little bit?).
|
# I'm not proud (I am a little bit?).
|
||||||
SIEVE = []
|
|
||||||
PROOF = [] # :)
|
PROOF = [] # :)
|
||||||
|
|
||||||
def factor(n):
|
def factor(n):
|
||||||
@@ -18,12 +17,13 @@ def factor(n):
|
|||||||
def print_sieve(sieve):
|
def print_sieve(sieve):
|
||||||
for n, prime in enumerate(sieve):
|
for n, prime in enumerate(sieve):
|
||||||
if n >= 2 and prime:
|
if n >= 2 and prime:
|
||||||
click.echo(f"[[{n}]] is prime.")
|
|
||||||
|
click.echo(f"{n}")
|
||||||
|
|
||||||
def is_prime(n):
|
def is_prime(n):
|
||||||
SIEVE = [True for n in range(0, n+1)]
|
sieve = [True for n in range(0, n+1)]
|
||||||
upto = math.ceil(math.sqrt(n))
|
upto = math.ceil(math.sqrt(n))
|
||||||
for i, _ in enumerate(SIEVE):
|
for i, _ in enumerate(sieve):
|
||||||
# click.echo(f"i: {i}")
|
# click.echo(f"i: {i}")
|
||||||
if i < 2:
|
if i < 2:
|
||||||
continue
|
continue
|
||||||
@@ -31,7 +31,7 @@ def is_prime(n):
|
|||||||
if i > upto:
|
if i > upto:
|
||||||
break
|
break
|
||||||
# If this is a known composite, then we've already crossed off its multiples when we iterated over its primes.
|
# If this is a known composite, then we've already crossed off its multiples when we iterated over its primes.
|
||||||
if not SIEVE[i]:
|
if not sieve[i]:
|
||||||
continue
|
continue
|
||||||
# for j in range(2, math.ceil(math.sqrt(n) + 1)):
|
# for j in range(2, math.ceil(math.sqrt(n) + 1)):
|
||||||
for j in range(2, n):
|
for j in range(2, n):
|
||||||
@@ -40,10 +40,10 @@ def is_prime(n):
|
|||||||
break
|
break
|
||||||
PROOF.append(f"[[{i*j}]] is composite: {i} * {j}.")
|
PROOF.append(f"[[{i*j}]] is composite: {i} * {j}.")
|
||||||
try:
|
try:
|
||||||
SIEVE[i*j] = False
|
sieve[i*j] = False
|
||||||
except IndexError:
|
except IndexError:
|
||||||
continue
|
continue
|
||||||
return (n >= 2 and SIEVE[n], SIEVE)
|
return (n >= 2 and sieve[n], sieve)
|
||||||
|
|
||||||
class AgoraCmd(click.Command):
|
class AgoraCmd(click.Command):
|
||||||
def format_help(self, ctx, formatter):
|
def format_help(self, ctx, formatter):
|
||||||
@@ -77,7 +77,7 @@ def prime(n):
|
|||||||
click.echo(f"{n} is not prime. Want proof? :)")
|
click.echo(f"{n} is not prime. Want proof? :)")
|
||||||
click.echo("\n".join([line for line in PROOF if f'[[{n}]]' in line]))
|
click.echo("\n".join([line for line in PROOF if f'[[{n}]]' in line]))
|
||||||
|
|
||||||
print_sieve(sieve)
|
click.echo(print_sieve(sieve))
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
prime()
|
prime()
|
||||||
|
|||||||
Reference in New Issue
Block a user