Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
26
Task/Fermat-numbers/Python/fermat-numbers-1.py
Normal file
26
Task/Fermat-numbers/Python/fermat-numbers-1.py
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
def factors(x):
|
||||
factors = []
|
||||
i = 2
|
||||
s = int(x ** 0.5)
|
||||
while i < s:
|
||||
if x % i == 0:
|
||||
factors.append(i)
|
||||
x = int(x / i)
|
||||
s = int(x ** 0.5)
|
||||
i += 1
|
||||
factors.append(x)
|
||||
return factors
|
||||
|
||||
print("First 10 Fermat numbers:")
|
||||
for i in range(10):
|
||||
fermat = 2 ** 2 ** i + 1
|
||||
print("F{} = {}".format(chr(i + 0x2080) , fermat))
|
||||
|
||||
print("\nFactors of first few Fermat numbers:")
|
||||
for i in range(10):
|
||||
fermat = 2 ** 2 ** i + 1
|
||||
fac = factors(fermat)
|
||||
if len(fac) == 1:
|
||||
print("F{} -> IS PRIME".format(chr(i + 0x2080)))
|
||||
else:
|
||||
print("F{} -> FACTORS: {}".format(chr(i + 0x2080), fac))
|
||||
137
Task/Fermat-numbers/Python/fermat-numbers-2.py
Normal file
137
Task/Fermat-numbers/Python/fermat-numbers-2.py
Normal file
|
|
@ -0,0 +1,137 @@
|
|||
'''Fermat numbers'''
|
||||
|
||||
from itertools import count, islice
|
||||
from math import floor, sqrt
|
||||
|
||||
|
||||
# fermat :: Int -> Int
|
||||
def fermat(n):
|
||||
'''Nth Fermat number.
|
||||
Nth term of OEIS A000215.
|
||||
'''
|
||||
return 1 + (2 ** (2 ** n))
|
||||
|
||||
|
||||
# fermats :: () -> [Int]
|
||||
def fermats():
|
||||
'''Non-finite series of Fermat numbers.
|
||||
OEIS A000215.
|
||||
'''
|
||||
return (fermat(x) for x in enumFrom(0))
|
||||
|
||||
|
||||
# --------------------------TEST---------------------------
|
||||
# main :: IO ()
|
||||
def main():
|
||||
'''First 10 Fermats, and factors of first 7.'''
|
||||
|
||||
print(
|
||||
fTable('First ten Fermat numbers:')(str)(str)(
|
||||
fermat
|
||||
)(enumFromTo(0)(9))
|
||||
)
|
||||
|
||||
print(
|
||||
fTable('\n\nFactors of first seven:')(str)(
|
||||
lambda xs: repr(xs) if 1 < len(xs) else '(prime)'
|
||||
)(primeFactors)(
|
||||
take(7)(fermats())
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
# -------------------------DISPLAY-------------------------
|
||||
|
||||
# fTable :: String -> (a -> String) ->
|
||||
# (b -> String) -> (a -> b) -> [a] -> String
|
||||
def fTable(s):
|
||||
'''Heading -> x display function -> fx display function ->
|
||||
f -> xs -> tabular string.
|
||||
'''
|
||||
def go(xShow, fxShow, f, xs):
|
||||
ys = [xShow(x) for x in xs]
|
||||
w = max(map(len, ys))
|
||||
return s + '\n' + '\n'.join(map(
|
||||
lambda x, y: y.rjust(w, ' ') + ' -> ' + fxShow(f(x)),
|
||||
xs, ys
|
||||
))
|
||||
return lambda xShow: lambda fxShow: lambda f: lambda xs: go(
|
||||
xShow, fxShow, f, xs
|
||||
)
|
||||
|
||||
|
||||
# -------------------------GENERIC-------------------------
|
||||
|
||||
# enumFrom :: Enum a => a -> [a]
|
||||
def enumFrom(x):
|
||||
'''A non-finite stream of enumerable values,
|
||||
starting from the given value.
|
||||
'''
|
||||
return count(x) if isinstance(x, int) else (
|
||||
map(chr, count(ord(x)))
|
||||
)
|
||||
|
||||
|
||||
# enumFromTo :: Int -> Int -> [Int]
|
||||
def enumFromTo(m):
|
||||
'''Enumeration of integer values [m..n]'''
|
||||
def go(n):
|
||||
return list(range(m, 1 + n))
|
||||
return lambda n: go(n)
|
||||
|
||||
|
||||
# primeFactors :: Int -> [Int]
|
||||
def primeFactors(n):
|
||||
'''A list of the prime factors of n.
|
||||
'''
|
||||
def f(qr):
|
||||
r = qr[1]
|
||||
return step(r), 1 + r
|
||||
|
||||
def step(x):
|
||||
return 1 + (x << 2) - ((x >> 1) << 1)
|
||||
|
||||
def go(x):
|
||||
root = floor(sqrt(x))
|
||||
|
||||
def p(qr):
|
||||
q = qr[0]
|
||||
return root < q or 0 == (x % q)
|
||||
|
||||
q = until(p)(f)(
|
||||
(2 if 0 == x % 2 else 3, 1)
|
||||
)[0]
|
||||
return [x] if q > root else [q] + go(x // q)
|
||||
|
||||
return go(n)
|
||||
|
||||
|
||||
# take :: Int -> [a] -> [a]
|
||||
# take :: Int -> String -> String
|
||||
def take(n):
|
||||
'''The prefix of xs of length n,
|
||||
or xs itself if n > length xs.
|
||||
'''
|
||||
return lambda xs: (
|
||||
xs[0:n]
|
||||
if isinstance(xs, (list, tuple))
|
||||
else list(islice(xs, n))
|
||||
)
|
||||
|
||||
|
||||
# until :: (a -> Bool) -> (a -> a) -> a -> a
|
||||
def until(p):
|
||||
'''The result of repeatedly applying f until p holds.
|
||||
The initial seed value is x.
|
||||
'''
|
||||
def go(f, x):
|
||||
v = x
|
||||
while not p(v):
|
||||
v = f(v)
|
||||
return v
|
||||
return lambda f: lambda x: go(f, x)
|
||||
|
||||
|
||||
# MAIN ---
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue