-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem4.py
More file actions
33 lines (30 loc) · 862 Bytes
/
problem4.py
File metadata and controls
33 lines (30 loc) · 862 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# Melissa Mangos
# Project Euler Problem 4
# Find the largest palindrome made from the product of two 3-digit numbers.
# Answer: 993 x 913 = 906609
import math
# Checks if a number is a palindrome or not
def is_palindrome(num):
# Gets the closest power of 10
power = (int)(math.floor(math.log10(num)))
high = pow(10, power)
# Starts the low value at 1
low = 1
# On first loop, checks first digit to last then goes
# to second and second last , etc.
while (high >= low):
if (((num / high) % 10) != ((num / low) % 10)):
return False
high = high / 10
low = low * 10
return True
def largest_product():
largest = 0
# Decreasing by 3 digit numbers
for i in range (999, 101, -1):
for j in range (i, 101, -1):
product = i*j
if (is_palindrome(product) and largest < product):
largest = product
return largest
print(largest_product())