Skip to content

Instantly share code, notes, and snippets.

@betaprojects
Created July 25, 2021 19:54
Show Gist options
  • Save betaprojects/00368c04eca1812f4b990eab45ef670f to your computer and use it in GitHub Desktop.
Save betaprojects/00368c04eca1812f4b990eab45ef670f to your computer and use it in GitHub Desktop.
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])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment