Solution: 233168
My code:
multiples = sum(num for num in range(1, 1000) if num % 3 == 0 or num % 5 == 0)
print(multiples)
Explanation:
When I saw this problem, I thought of using the modulus operator (%). This allows for the discovery of numbers that had no remainder when divided by 3 or 5 (meaning they are multiples of 3 and 5). Then, I created a for loop that would run through every number from 1 to 1000 checking to see if they were a multiple using that if statement mentioned previously. I then used sum() to add up all the discovered multiples and set the entire line under the variable "multiples".
Solution: 4613732
My code:
def fibonacci(limit):
x, y = 1, 2
total = 0
while y <= limit:
if y % 2 == 0:
total += y
x, y = y, x + y
return total
limit = 4000000
print(fibonacci(limit))
Explanation:
Looking at this problem, I could immediately use the (%) operator to eliminate any numbers that didn't perfectly divide by 2 as they are not even. I defined a function "fibonacci" of limit and set the limit to 4 million. I then set the two variables x and y to 1 and 2 as they are the first terms of the sequence. Then I created a while loop which would run until the limit was reached and used the y % 2 == 0 to essentially say that if the new term divides by 2, then it should be added to the total. The last part of the function was to define how x and y would change through out the sequence. x would become y, and y would become x + y.
Solution: 6857
My code:
import math
def prime_factors(n):
factors_list = []
while n % 2 == 0:
factors_list.append(2)
n //= 2
#starts at 3 and moves up to each odd number after that, checking its square root if it has one.
#adds the approved factor to the list
for y in range(3, int(math.sqrt(n)) + 1, 2):
while n % y == 0:
factors_list.append(y)
n //= y
if n > 2:
factors_list.append(n)
return factors_list
num = int(input("Type number here:"))
(f"The prime factors of {num} are {prime_factors(num)}")
Explanation:
Firstly, I imported the math library as I will use it later. Next, I defined the function prime_factors and created an empty list to store the factors in called factors_list. I then created a while loop that performs the simple logical act that if the number is even, 2 is one of its prime factors and will be added to the list. The next part of the function runs through every factor after 3 going up by 2 as that keeps it odd. The middle part of that line prevents redundancy as any factors greater than the square root of the number would have already been checked. The next while loop states that if the number divded by its factor leaves no remainder, it is added to the list.
Solution: 906609
My code:
import math
def palindrome_3_digits():
palindromes = []
for x in range(100,1000):
for y in range(100,1000):
z = x * y #this is the product
if str(z) == str(z)[::-1]: #checks that it is a palindrome by reversing the string and equaling to each other
palindromes.append(z)
return palindromes
big_palindrome = max(palindrome_3_digits())
print(big_palindrome)
Explanation:
I imported the math library. I then created an empty list to store all the palindromes then I created two variables to store one 3 digit number and the other stores the other 3 digit number. I then converted the number to a string and used reverse list syntax in an if statement to check if the string is the same as itself when reversed in order. If so, it is added to the list. I then created another variable to store the maximum value of the palindromes list and printed it.