Skip to content

Instantly share code, notes, and snippets.

@betaprojects
betaprojects / Project-Euler-Problem-19.py
Last active July 15, 2021 05:02
Project Euler & HackerRank problem 19 solution: Counting Sundays solved using Python
import datetime as dt
delta, c, targetDOW, targetDate = dt.timedelta(days=1), 0, 6, 1
yS, mS, dS, yE, mE, dE = 1901,1,1, 2000,12,31
yearDelta = yE-yS # number of years
yS = (yS%400) + 400 # normalized and non-zero
a = dt.datetime(yS, mS, dS)
b = dt.datetime(yS+yearDelta, mE, dE)
while a <= b:
@betaprojects
betaprojects / Project-Euler-Problem-15.py
Last active September 11, 2020 21:07
Project Euler & HackerRank problem 15 solution: Lattice paths - solved using Python
from math import factorial as f
n, m = map(int, input("Enter dimensions (separate by space)?").split())
print ("Routes through a", n, "x", m, "grid", f(n+m) // f(n) // f(m))
@betaprojects
betaprojects / Project-Euler-Problem-1.py
Last active September 11, 2020 21:14
Project Euler & HackerRank problem 1 solution: Multiples of 3 and 5 - solved using Python
def sumn(n, d): # Sum natural numbers ≤ n that are divisible by d
n //= d
return d*n*(n+1) // 2
L = int(input('Enter an upper bound? '))
a, b = 3, 5
s = sumn(L-1, a) + sumn(L-1, b) - sumn(L-1, a*b)
print ("Sum of multiples of", a, "or", b, "below", L, "=", s)
@betaprojects
betaprojects / Project-Euler-Problem-2.py
Last active September 11, 2020 21:14
Project Euler & HackerRank problem 2 solution: Even Fibonacci numbers - solved using Python
s, x, y = 0, 0, 2
L = int(input('Sum of even Fibonacci numbers <'))
while y < L:
s, x, y = s+y, y, 4*y+x
print ("is", s)
@betaprojects
betaprojects / Project-Euler-Problem-5.py
Last active September 11, 2020 21:16
Project Euler & HackerRank problem 5 solution: Smallest multiple - solved using Python
from math import gcd
from functools import reduce
def LCM(a, b):
return a // gcd(a, b) * b
N = int(input("The LCM for numbers 1 through "))
print ("is", reduce(LCM, range(N//2+1, N+1)))
@betaprojects
betaprojects / Project-Euler-Problem-4.py
Last active September 11, 2020 21:07
Project Euler & HackerRank problem 4 solution: The largest palindrome product - solved using Python
def is_palindromic(n): n=str(n); return n==n[::-1]
dd = dict()
for i in range(101, 1000):
for j in range(121, 1000, (1 if i%11==0 else 11)):
p = i*j
if p >= 101101 and is_palindromic(p): dd[p]=1
lst = sorted(list(dd), reverse=True)
K = int(input("Enter a number between 101101 and 999999? "))
q = min(lst, key=lambda x:x>=K)
@betaprojects
betaprojects / Project-Euler-Problem-3.py
Last active September 11, 2020 21:06
Project Euler & HackerRank problem 3 solution: Largest prime factor - solved using Python
n = int(input('The largest prime factor of '))
while n%2==0: n//= 2 # Remove all even factors
d = 3
while d*d <= n:
if n % d == 0:
n//= d
else:
d+= 2
print ("is", 2 if n==1 else n)
@betaprojects
betaprojects / Project-Euler-Problem-40.py
Last active April 24, 2021 20:48
Project Euler & HackerRank problem 40 solution: Champernowne’s constant - solved using Python
q = [9*(x+1) * pow(10, x) for x in range(20)]
def d(n): # find the digit at position n in Champernowne's constant
i = 0
while n>q[i]: n-= q[i]; i+= 1
L, d = divmod((n-1), i+1)
return int(str(pow(10, i)+L)[d])
n = input("Enter some indexes, separated by a space? ")
m = 1
for ci in map(int, n.split()):
m*= d(ci)
@betaprojects
betaprojects / Project-Euler-Problem-35.md
Last active July 23, 2021 07:30
Project Euler & HackerRank problem 35 solution: Find the number of circular primes below one million - solved using Python
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-6.py
Last active September 11, 2020 21:12
Project Euler & HackerRank problem 6 solution: Compare the sum of squares and sum squared - solved using Python
# -*- coding: utf-8 -*-
n = int(input('first n natural numbers, n ='))
print ("Difference: (Σn)² - Σn² =", n*(n-1)*(n+1)*(3*n+2)//12)