Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,6 @@
def perf1(n):
sum = 0
for i in range(1, n):
if n % i == 0:
sum += i
return sum == n

View file

@ -0,0 +1,22 @@
from itertools import chain, cycle, accumulate
def factor2(n):
def prime_powers(n):
# c goes through 2, 3, 5, then the infinite (6n+1, 6n+5) series
for c in accumulate(chain([2, 1, 2], cycle([2,4]))):
if c*c > n: break
if n%c: continue
d,p = (), c
while not n%c:
n,p,d = n//c, p*c, d + (p,)
yield(d)
if n > 1: yield((n,))
r = [1]
for e in prime_powers(n):
r += [a*b for a in r for b in e]
return r
def perf4(n):
"Using most efficient prime factoring routine from: http://rosettacode.org/wiki/Factors_of_an_integer#Python"
return 2 * n == sum(factor2(n))

View file

@ -0,0 +1,6 @@
def perf2(n):
return n == sum(i for i in range(1, n) if n % i == 0)
print (
list(filter(perf2, range(1, 10001)))
)

View file

@ -0,0 +1,35 @@
'''Perfect numbers'''
from math import sqrt
# perfect :: Int - > Bool
def perfect(n):
'''Is n the sum of its proper divisors other than 1 ?'''
root = sqrt(n)
lows = [x for x in enumFromTo(2)(int(root)) if 0 == (n % x)]
return 1 < n and (
n == 1 + sum(lows + [n / x for x in lows if root != x])
)
# main :: IO ()
def main():
'''Test'''
print([
x for x in enumFromTo(1)(10000) if perfect(x)
])
# GENERIC -------------------------------------------------
# enumFromTo :: (Int, Int) -> [Int]
def enumFromTo(m):
'''Integer enumeration from m to n.'''
return lambda n: list(range(m, 1 + n))
if __name__ == '__main__':
main()