1. PE Q1-25#

To solve PE problems so far, I include three approaches:

  • Python OOP

  • Python FP

  • MMA (FP primarily)

Pre-running Python functions:

  • After I solved many questions using Python, I found some functions were repeated, like Prime-Check, Fibonacci. Then I built this part to store these functions for pre-running.

%run _static/PE_pre_running.py

1.1. PE 1#

#1 Multiples of 3 or 5
sum_num=0
list_num=[]
for i in range(1, 999//3):
    m=i*3
    list_num.append(m)
for i in range(1, 999//5):
    m=i*5
    if m not in list_num:
        list_num.append(m)
    else:
        pass
for num in list_num:
    sum_num+=num
sum_num

1.2. PE 2#

#2 Even Fibonacci Numbers
num=0
sum_num=0
list_fib=[]
for i in range(3000000):
    num=fastFib(i)
    list_fib.append(num)
for num in list_fib:
    if num//2==num/2:
        sum_num+=num
sum_num

1.3. PE 4#

#4 Largest Palindrome Product
m=999
n=999
list_palin=[]
for i in range(900):
    for i in range(900):
        print(m*n)
        n-=1
        if palindrome(m*n):
            print('Palindrome')
            list_palin.append(m*n)
    m-=1
    n=999
max(list_palin)

1.4. PE 5#

#5 Smallest Multiple
2520*11*13*17*19*2

1.5. PE 6#

# 6 Sum Square Difference
nums=[]
sums=0
n=1
for i in range(100):
    nums.append(n)
    n+=1
for num in nums:
    sums+=num
sums**2
# 6 Sum Square Difference
nums=[]
sums=0
n=1
for i in range(100):
    nums.append(n)
    n+=1
for num in nums:
    sums+=num**2
sums

#6 results
25502500-338350

1.6. PE 7#

#7 10001st Prime
fd_prime(1)

1.7. PE 8#

#8 Largest Multiple In a Series
num=7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450
output=list(map(int, str(num)))
nums=[0, 12]
list_num=[]
maxval=0
val=1
while nums[1]<=999:
    num0=nums[0]
    num1=nums[1]
    n=num0
    while n<=num1:
        list_num.append(output[n])
        n+=1
    for i in range(13):
        val*=list_num[i]
    if val>maxval:
        maxval=val
    val=1
    nums[0]+=1
    nums[1]+=1
    list_num=[]
maxval

1.8. PE 9#

#9 Special Pythagorean Triplet
pythagoreanTriplets(750)

1.9. PE 10#

#10 Summation of Primes
sum_prime=0
for i in range(2,2000000):
    if prime(i):
        sum_prime+=i
print(sum_prime)

1.10. PE 11#

#11 Largest Product in a Grid
87*97*94*89

1.11. PE 12#

#12 Highly Divisible Triangular Number
seq=triangular_seq(12375)
facs=[]
for triangular in seq:
    facs.append(num_fac(triangular))
    if facs[-1]>=500:
        print(len(facs))
        break
max(facs)
print(len(facs))
max(seq)

1.12. PE 13#

#13 Large Sum
import requests
from bs4 import BeautifulSoup
response = requests.get("https://projecteuler.net/problem=13")  # Send a GET request to the URL
soup = BeautifulSoup(response.content, "html.parser")   # Parse the HTML content
numbers = soup.find("div", class_="problem_content").text.strip()  # Find the content of interest (in this case, the numbers)

numbers_list = numbers.split('\n')  # Split the numbers into a list, assuming each number is separated by a newline character
numbers_list = [num for num in numbers_list if num] # Filter out any empty strings or irrelevant text
numbers_list = numbers_list[1:101] # Ensure that we only take the first 100 numbers

sum_num=0
sets = [int(num) for num in numbers_list]  # Convert the numbers from strings to integers and store them in a list
for num in sets:
    sum_num+=num
print(sum_num)

1.13. PE 14#

#14 Longest Collatz Sequence
n=1
max_len=1
for i in range(1000000):
    culen=collatz_length(n)
    if culen==525:
        print(n)
    if culen>max_len:
        max_len=culen
    n+=1

1.14. PE 15#

#15 Lattice Paths
n = 20
result = paths(n)
print(result)

1.15. PE 16#

#16 Power Digit Sum
num=[10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376]
output=list(map(int, str(num[0])))
sum_num=0
for number in output:
    sum_num+=number
sum_num

1.16. PE 17#

#17 Number Letter Counts
solve(1000)

1.17. PE 18#

#18 Maximum Path Sum I
l1=[75]
l2=[95, 64]
l3=[17, 47, 82]
l4=[18, 35, 87, 10]
l5=[20, 4, 82, 47, 65]
l6=[19, 1, 23, 75, 3, 34]
l7=[88, 2, 77, 73, 7, 63, 67]
l8=[99, 65, 4, 28, 6, 16, 70, 92]
l9=[41, 41, 26, 56, 83, 40, 80, 70, 33]
l10=[41, 48, 72, 33, 47, 32, 37, 16, 94, 29]
l11=[53, 71, 44, 65, 25, 43, 91, 52, 97, 51, 14]
l12=[70, 11, 33, 28, 77, 73, 17, 78, 39, 68, 17, 57]
l13=[91, 71, 52, 38, 17, 14, 91, 43, 58, 50, 27, 29, 48]
l14=[63, 66, 4, 68, 89, 53, 67, 30, 73, 16, 69, 87, 40, 31]
l15=[4, 62, 98, 27, 23, 9, 70, 98, 73, 93, 38, 53, 60, 4, 23]
triangle=[l1, l2, l3, l4, l5, l6, l7, l8, l9, l10, l11, l12, l13, l14, l15]
for i in range(len(triangle) - 2, -1, -1):
    for j in range(len(triangle[i])):
        triangle[i][j] += max(triangle[i + 1][j], triangle[i + 1][j + 1])
max_total = triangle[0][0]
print(max_total)

1.18. PE 19#

#19 Counting Sundays
import datetime
count=0
for year in range(1901, 2001):
    for month in range(1, 13):
        if datetime.date(year, month, 1).weekday()==6:
            count+=1
print(count)

1.19. PE 20#

#20 Factorial Digit Sum
num=fact(100)
output=list(map(int, str(num)))
sum_num=0
for number in output:
    sum_num+=number
sum_num

1.20. PE 21#

# 21 Amicable Numbers
amic=[]
for i in range(1,10000):
    if amicable(i):
        amic.append(i)
sum(amic)

1.21. PE 22#

#22 Names Scores
import requests
response = requests.get("https://projecteuler.net/resources/documents/0022_names.txt")   # Send a GET request to the URL
content = response.text     # Read the content of the response
names = [name.strip('"') for name in content.split(',')]  # Split the content by comma and remove double quotes
names.sort()

value={'A':1, 'B':2, 'C':3, 'D':4, 'E':5, 'F':6, 'G':7, 'H':8, 'I':9, 'J':10, 'K':11, 'L':12, 'M':13, 'N':14, 'O':15, 'P':16, 'Q':17, 'R':18, 'S':19, 'T':20, 'U':21, 'V':22, 'W':23, 'X':24, 'Y':25, 'Z':26}
tot_score=0
place=1
for name in names:
    score=0
    for letter in name:
        score+=value[letter]
    score*=place
    tot_score+=score
    place+=1
tot_score
def factors(n):
    facs = {1}
    for i in range(2, int(n**0.5)+1):
        if n%i==0:
            facs.add(i)
            facs.add(n // i)
    return facs
def abundant(n):
    return sum(factors(n)) > n
def sum_abundant(n, abundant_numbers):
    for ab in abundant_numbers:
        if (n - ab) in abundant_numbers:
            return True
    return False
abundant_numbers = {i for i in range(12, 28124) if abundant(i)}
non_abundant_sums = [i for i in range(1, 28124) if not sum_abundant(i, abundant_numbers)]
result = sum(non_abundant_sums)
print(result)

1.22. PE 23#

# 23 Non-Abundant Sums
def factors(n):
    facs=[1]
    for i in range(2,int(n**0.5)+1):  # 非常耗时,缩小范围
        if n%i==0:
            facs.append(i)
            facs.append(n//i)
    return facs

def abundant(n):
    return sum(factors(n))>n

def sum_abundant(n,abundant_numbers):
    for ab in abundant_numbers:
        if (n - ab) in abundant_numbers:
            return True
    return False
abundant_numbers = {i for i in range(12, 28124) if abundant(i)}
non_abundant_sums = [i for i in range(1, 28124) if not sum_abundant(i, abundant_numbers)]

sum(non_abundant_sums) # Error: smaller by 258348

1.23. PE 24#

#24 Lexicographic Permutations
import itertools
permutations = list(itertools.permutations(range(10)))
millionth_permutation = int(''.join(map(str, permutations[999999])))
millionth_permutation

1.24. PE 25#

#25 1000 Digit Fibonacci Number
fastFib(4781)
length(1731403589222002957294429346518895723709506082039860786521776201969997425190002580820509546929820263058457975554718584767897610032505496877507394744099980978096953962557220579370143960032185044755214925165471985133186165562568372954212266775160479690961083330407371040988504455586839093910684051542272860673487922394300583546141201071181523567766482697651998576131907383462584505182933770187893410602855362990044627645611957937480375221691136281665191108770486046208365987457152897898978151032624598781944378501953255407873508942191356136619446735012761729556373994129675990036580541567079157500214451848590106380276929004769735246144938528628285344388350842365138183068393693524442382719001304601982880025898373942014814103022093332575900147730351419594078092359452500825422173232850278854098501036548721303293491571977471641513836658780413653798660841910284032061829208989654332339843602086914702633919395534251088908273898020000096184168171559429402858006596531459999394154832243826895270097988797)