Added solutions N = 150-200

master
Tibor Bizjak 2023-03-25 17:24:17 +01:00
parent 63a76b01f8
commit 56f12e1fd2
17 changed files with 412 additions and 0 deletions

View File

@ -0,0 +1,24 @@
#!/usr/bin/env python3
from fractions import Fraction
def cut(state, i):
return state[:i] + (state[i]-1,) + \
tuple(n + 1 for n in state[i+1:])
def search(state=(1, 0, 0, 0, 0), cache={}):
if state in cache:
return cache[state]
if sum(state) == state[-1] == 1:
return 1
r = 0
for i, p in enumerate(state):
if not p:
continue
r += Fraction(p, sum(state)) * \
search(cut(state, i), cache)
r += (sum(state) == 1)
cache[state] = r
return r
print(float(round(search(), 6) - 2))

View File

@ -0,0 +1,33 @@
#!/usr/bin/env python3
def calc(n, dig=1, verbose=False):
r = 0
for place in range(len(str(n))):
a = (n - (n % 10**(place+1))) // 10
b = 0
d = int(str(n)[-place-1])
if d == dig:
b = (n % 10**place) + 1
elif d > dig:
b = 10**place
if verbose:
print(place, a, b, a+b)
r += a + b
return r
def search(dig, pw=2, num=0):
if pw == 0:
return sum(num + d for d in range(10) if num+d == calc(num+d, dig))
r = 0
for d in range(10):
newnum = num + d * 10**pw
maxnum = num + (d + 1) * 10**pw - 1
if maxnum < calc(newnum, dig):
continue
if calc(maxnum, dig) < newnum:
continue
r += search(dig, pw-1, newnum)
return r
depth = 10
print(sum(search(d, depth) for d in range(1, 10)))

18
158_strings.py 100644
View File

@ -0,0 +1,18 @@
#!/usr/bin/env python3
from math import factorial
def bin(n, k):
if k > n:
return 0
return factorial(n) // (factorial(n-k)*factorial(k))
def strings(k):
k -= 2
s = 0
for inter in range(25):
for i in range(0,k+1):
s += (25-inter) * 2**i * bin(inter, i) * bin(24-inter, k-i)
return s
print(max((2**k - k - 1) * bin(26, k) for k in range(2, 27)))

View File

@ -0,0 +1,41 @@
#!/usr/bin/env python3
from math import log
digs = 5
mask = 10**digs
sieve = [1] * mask
for n in range(1, mask):
if n%2 and n%5:
sieve[n] = (sieve[n-1] * n) % mask
else:
sieve[n] = sieve[n-1]
def numf(n, N):
return sum(N // (n**pw) for pw in range(1, int(log(N) / log(n))+1))
def numof2(N):
return numf(2, N) - numf(5, N)
def roots(N):
a = 1
while a <= N:
b = 1
while a * b <= N:
yield a * b
b *= 2
a *= 5
def main(N):
r = 1
p = sieve[-1]
for root in roots(N):
d = N // root
r *= pow(p, d // mask, mask)
r *= sieve[d % mask]
r %= mask
r *= pow(2, numof2(N), mask)
return r % mask
print(main(10**12))

View File

@ -0,0 +1,30 @@
#!/usr/bin/env python3
from itertools import product
from math import factorial
def bin(n, k):
return factorial(n) // (factorial(n-k) * factorial(k))
digs = list(map(str, range(10))) + list("ABCDEF")
cache = {val : {0 : all(val)} for val in product([True, False], repeat=3)}
def f(i, flags=(False, False, False), fst=1):
if i in cache[flags] and (not fst or not i):
return cache[flags][i]
r = 13 * f(i-1, flags, 0)
r += sum(f(i-1, flags[:n] + (True,) + flags[n+1:], 0) for n in range(fst, 3))
if not fst:
cache[flags][i] = r
return r
def dec2hex(n):
r = str()
while n:
r = r + digs[n % 16]
n //= 16
return r[::-1]
r = sum(f(i) for i in range(1, 17))
print(dec2hex(r))

View File

@ -0,0 +1,12 @@
#!/usr/bin/env python3
cache = [[{0 : 1} for b in range(10)] for a in range(10)]
def f(i, a=0, b=0, fst=1):
if i in cache[a][b]:
return cache[a][b][i]
r = sum((f(i-1, b, n, 0) for n in range(fst, 10-a-b)))
cache[a][b][i] = r
return r
print(f(20))

View File

@ -0,0 +1,48 @@
#!/usr/bin/env python3
from fractions import Fraction
from itertools import combinations
def solve(line1, line2):
x1, y1 = line1[0][0] - line1[1][0], line1[0][1] - line1[1][1]
x2, y2 = -line2[0][0] + line2[1][0], -line2[0][1] + line2[1][1]
xv, yv = line2[1][0] - line1[1][0], line2[1][1] - line1[1][1]
det = x1*y2 - y1*x2
if det == 0:
return None
p = det // abs(det)
det = abs(det)
n = p * (xv*y2 - yv*x2)
if not (0 < n < det) or not (0 < p * (x1*yv - y1*xv) < det):
return None
return (line1[1][0] + Fraction(n, det) * x1, line1[1][1] + Fraction(n, det) * y1)
l1 = (27, 44), (12, 32)
l2 = (46, 53), (17, 62)
l3 = (46, 70), (22, 40)
def blum(n):
s = 290797
for i in range(n):
s = pow(s, 2, 50515093)
yield s % 500
N = 5000
nums = list(blum(N*4))
lines = []
for i in range(0, len(nums)-3, 4):
line = (nums[i], nums[i+1]), (nums[i+2], nums[i+3])
lines.append(line)
points = []
for a, b in combinations(lines, 2):
p = solve(a, b)
if p == None:
continue
points.append(p)
print(len(set(points)))

View File

@ -0,0 +1,22 @@
#!/usr/bin/env python3
def search(i, k, fst, dig=None, r=0, num=str()):
if dig == None:
dig = fst
if i == 0:
return False
n = k*dig + r
if n < 10 and fst == n and dig != 0:
return str(dig) + num
return search(i-1, k, fst, n%10, n//10, str(dig) + num)
places = 100
s = 0
for k in range(1, 10):
for dig in range(1, 10):
n = search(places, k, dig)
if n:
r = places // len(n)
for i in range((1 + (len(n)==1)), r+1):
s += int(i*n) % 10**5
print(s % 10**5)

View File

@ -0,0 +1,20 @@
#!/usr/bin/env python3
from math import ceil
digs = 20
ln = 9**2 * digs + 1
mod = 10**9
sums = [0] * ln
count = [1] + [0] * (ln - 1)
for pw in range(digs):
for dsum in range(9**2 * pw, -1, -1):
for dig in range(1, 10):
sums[dsum + dig**2] += dig * 10**pw * count[dsum] + sums[dsum]
count[dsum + dig**2] += count[dsum]
print(sum(sums[i**2] for i in range(int(ceil(ln**0.5)))) % mod)

View File

@ -0,0 +1,9 @@
#!/usr/bin/env python3
from math import sqrt
b = 10**6
t = 0
for n in range(1, b//4):
t += int(sqrt(b + n**2) - n)//2
print(t)

View File

@ -0,0 +1,18 @@
#!/usr/bin/env python3
def divisors(n):
sieve = [1]*n
for p in range(2, n):
if sieve[p] > 1:
continue
pw = 1
while p**pw < n:
for i in range(p**pw, n, p**pw):
sieve[i] *= (pw+1)
sieve[i] //= pw
pw += 1
return sieve
n = 10**6 // 4
divs = divisors(n+1)
print(len([0 for i in divs if 0 < i//2 <= 10]))

View File

@ -0,0 +1,42 @@
#!/usr/bin/env python3
conds = ["any", "both", "nine"]
cache = {c : {n : dict() for n in range(10)} for c in conds}
def f(n, i, contains = "both"):
if i in cache[contains][n]:
return cache[contains][n][i]
if i == 1:
if contains == "both":
r = 0
elif contains == "any":
r = 1
elif contains == "nine":
r = (n == 9)
cache[contains][n][i] = r
return r
g = lambda x,y: f(x, i-1, y)
flag = contains
if n == 0 or n == 9:
if contains == "both":
flag = "nine"
elif contains == "nine" and n == 9:
flag = "any"
r = g(1, flag)
else:
r = g(n-1, contains) + g(n+1, contains)
cache[contains][n][i] = r
return r
places = 40
s = 0
for p in range(1, places+1):
s += sum(f(n, p) for n in range(1,10))
print(s)

View File

@ -0,0 +1,13 @@
#!/usr/bin/env python3
lim = 10**7
divcount = [1]*lim
count = 0
for n in range(2, lim):
for i in range(n, lim, n):
divcount[i] += 1
if divcount[n] == divcount[n-1]:
count += 1
print(count)

20
187_semiprimes.py 100644
View File

@ -0,0 +1,20 @@
#!/usr/bin/env python3
def primes(n):
count = 0
sieve = [False]*n
for p in range(2, n):
if sieve[p]:
continue
for i in range(p**2, n, p):
sieve[i] = True
np = 2*p
for p2 in range(2,p+1):
if np >= n:
break
if not sieve[p2]:
count += 1
np += p
return count
print(primes(10**8))

View File

@ -0,0 +1,11 @@
#!/usr/bin/env python3
a = 1777
k = 1855
n = 1
for i in range(k):
n = pow(a, n, 10**8)
print(n)

View File

@ -0,0 +1,26 @@
#!/usr/bin/env python3
from math import factorial
def bin(n, k):
return factorial(n) // (factorial(n-k) * factorial(k))
def late(n):
return 3**n - 2**n - 2**(n-1) * n
def absent2(n):
if n < 3:
return 0
middle = (n-3) * 2**(n-4) - sum(2**(n-4-i) * absent(i) for i in range(n-3))
left = sum(2**(n-4-i) * (i * 2**(i-1) - absent2(i)) for i in range(n-3))
right = (n-3) * 2**(n-4) + sum((n-3-i) * 2**(n-4-i) * (2**(i-1) - absent(i-1)) for i in range(1, n-3))
return right + left + middle
def absent(n):
if n < 3:
return 0
return 2**(n-3) + (n-3) * 2**(n-4) - sum(2**(n-4-i) * absent(i) for i in range(n-3))
n = 30
print(int(3**n - late(n) - absent(n) - absent2(n)))

View File

@ -0,0 +1,25 @@
#!/usr/bin/env python3
def primes(n):
sieve = [False] * n
yield 2
for p in range(3, n, 2):
if sieve[p]:
continue
for i in range(p**2, n, 2*p):
sieve[i] = True
yield p
def search(i=0, n=1, sign=1):
r = 0
while squares[i] * n <= N:
m = n * squares[i]
r += sign * (N // m)
r += search(i+1, m, sign * (-1))
i += 1
return r
N = 2**50
squares = [p**2 for p in primes(2**25)] + [N+1]
print(N - search())