Skip to content

Instantly share code, notes, and snippets.

@betaprojects
betaprojects / Project-Euler-Problem-44.py
Created July 25, 2021 19:54
Project Euler & HackerRank problem 44 solution: Find the pair of pentagonal numbers, Pj and Pk, for which their sum and difference is pentagonal and |Pk − Pj| is minimised - solved using Python
#HackerRank Project Euler 44 version
from math import sqrt
isp = lambda p: (sqrt(24*p+1)+1)/6 % 1 ==0
ps = [(3*i*i - i) // 2 for i in range(1, 1000005)]
n,k = map(int, input().split())
for n in range(k+1, n+1):
if isp(ps[n]+ps[n-k]) or isp(ps[n]-ps[n-k]): print (ps[n])
@betaprojects
betaprojects / Project-Euler-Problem-99.py
Last active August 14, 2021 00:59
Project Euler & HackerRank problem 99 solution: Find the largest exponential from a list of base/exponent pairs - solved using Python
# HackerRank solution
from math import log
v = []
for _ in range(int(input())):
a, b = map(int, input().split())
v.append((b*log(a), a,b))
k = int(input())
x, a, b = sorted(v)[k-1]
print (a, b)
@betaprojects
betaprojects / Project-Euler-Problem-99.py
Created July 16, 2021 05:23
Project Euler & HackerRank problem 99 solution: Find the largest exponential among thousands of base/exponent pairs.
from math import log
v = []
for _ in range(int(input())):
b, e = map(int, input().split())
v.append((e * log(b), b, e))
k = int(input())-1
b, e = sorted(v)[k][1:3]
print (b, e)
prime_sieve()
def prime_sieve(n):
    sieve = [True] * (n//2)
    for i in range(3,int(n**0.5)+1,2):
        if sieve[i//2]:
            sieve[i*i//2::i] = [False] * ((n-i*i-1)//(2*i)+1)
    return [2] + [2*i+1 for i in range(1,n//2) if sieve[i]]
@betaprojects
betaprojects / Project-Euler-Problem-47-Solution.py
Last active July 15, 2021 05:06
Project Euler & HackerRank problem 47 solution: Find the first four consecutive integers to have four distinct prime factors.
L, K = map(int, input().split())
L+= K
f=[0]*L
for i in range(2,L):
if f[i] == K:
c +=1
if c == K:
print (i-K+1)
c -= 1
else:
prime_sieve()

def prime_sieve(n):
    sieve = [True] * (n//2)
    for i in range(3,int(n**0.5)+1,2):
        if sieve[i//2]:
 sieve[i*i//2::i] = [False] * ((n-i*i-1)//(2*i)+1)
@betaprojects
betaprojects / Project-Euler-Problem-63.py
Last active July 15, 2021 09:45
Project Euler & HackerRank problem 63 solution: Find how many n-digit positive integers exist which are also an nth power - solved using Python
k = int(input())
for n in range(1,10):
if len(str(pow(n,k)))==k: print (pow(n,k))
@betaprojects
betaprojects / Project-Euler-14.py
Last active July 19, 2021 08:25
Project Euler & HackerRank problem 14 solution: Longest Collatz sequence
c = [1]
def collatz(_):
cx = [0, 1] + [0]*5000000
def d(n):
if n>5000000: return d((3*n + 1)//2 if n&1 else n//2) + 1
if not cx[n]: cx[n] = d((3*n + 1)//2 if n&1 else n//2) + 1
return cx[n]
m = 0
for n in range(2, 5000001):
q = d(n)
c = [1, 2, 3, 6, 7, 9, 18, 19, 25, 27, 54, 55, 73, 97, 129, 171, 231, 235, 313, 327, 649, 654, 655, 667,
703, 871, 1161, 2223, 2322, 2323, 2463, 2919, 3711, 6171, 10971, 13255, 17647, 17673, 23529, 26623,
34239, 35497, 35655, 52527, 77031, 106239, 142587, 156159, 216367, 230631, 410011, 511935, 626331,
837799, 1117065, 1126015, 1501353, 1564063, 1723519, 2298025, 3064033, 3542887, 3732423]
for _ in xrange(input()):
N = input()
print min(c[::-1], key=lambda x:x>N)
@betaprojects
betaprojects / Project-Euler-Problem-97.py
Last active July 15, 2021 04:58
Project Euler & HackerRank problem 97 solution: Calculate the last 12-digits of large number in the form A x B^C + D such as the non-Mersenne prime 28433 × 2^7830457+1
s = 0
m = 10**12
for _ in range(int(input())):
a, b, c, d = map(int, input().split())
s+= (a * pow(b, c, m) + d) % m
print(f'{s % m : 012d}')