def isOct(n): if not isPerfectSquare(4+12*n): return False num = int(sqrt(4 + 12*n)) if (num + 4) % 4 == 0: return True return False
def isPent(n): if not isPerfectSquare(1 + 24 * n): return False num = int(sqrt(1 + 24 * n)) if (num + 1) % 6 == 0: return True return False
def isHept(n): if not isPerfectSquare(9+40*n): return False num = int(sqrt(9 + 40*n)) if (num + 3) % 10 == 0: return True return False
def isHex(n): if not isPerfectSquare(1+8*n): return False num = int(sqrt(1 + 8*n)) if (num + 1) % 4 == 0: return True return False
def paths(M): count = 0 for c in range(3, M + 1): for ab in range(3, 2 * c): if c % 2 == 1 and ab % 2 == 1: continue elif isPerfectSquare(ab**2 + c**2): if ab < c: count += ab // 2 else: count += c + 1 - (ab + 1) // 2 return count
# Project Euler # Problem 206 # Concealed square import sys sys.path.append('..') from euler import isPerfectSquare from itertools import product l = [str(x) for x in range(10)] num = '1234567890' def splice(str1, str2): result = str1[0] for i in range(len(str2)): result += str2[i] + str1[i + 1] return result for p in product(l, repeat=9): string = splice(num, ''.join(p)) if isPerfectSquare(int(string)): break print(int(int(string)**(1 / 2)))
from euler import isPerfectSquare from math import sqrt import time ''' Need to solve b(b-1) / n(n-1) = 1/2 2b**2 - 2b = n**2 - n 2(b - 1/2)**2 = n**2 - n + 1/2 b = 0.5 + 0.5 * sqrt(n**2 - n + 1/2) ''' start = time.time() target = 10**1 n = target x = int(target * sqrt(2)) x += 1 - (x % 2) # make x odd only while not isPerfectSquare(2 * x**2 - 1): x += 2 #print(x) n = int(0.5 * (1 + sqrt(2 * x**2 - 1)) + 1 / 2) # + 1/2 for rounding print('n = ' + str(n)) b = int(0.5 * (1 + sqrt(2 * n**2 - 2 * n + 1)) + 1 / 2) print('b = ' + str(b)) print(b * (b - 1) / (n * (n - 1))) print('time taken: ' + str(time.time() - start) + ' seconds')
# Problem 80 # Square root digital expansion import sys sys.path.append('..') from euler import isPerfectSquare ''' Calculates the sum of the first d digits of sqrt(num) ''' def sqrtDecimalSum(num, d): target = num n = 1 for i in range(d): while (n + 1)**2 < target: n += 1 n *= 10 target *= 100 return str(n) ans = 0 for n in range(1, 101): if not isPerfectSquare(n): ans += sum([int(x) for x in sqrtDecimalSum(n, 100)]) print(ans)
def isSquare(n): if not isPerfectSquare(n**2): return False return True