Δημοσιεύτηκε: 23 Ιούλ 2011, 14:57
από Ilias95
Απλά θα είναι λίγο δύσκολο να το παρακολουθήσουμε αν απλά τα παραθέτουμε.
Anyway, οι παρακάτω λύσεις όλες σε python:

Problem 1: Add all the natural numbers below one thousand that are multiples of 3 or 5.
Spoiler: show
Κώδικας: Επιλογή όλων
# If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9.
# The sum of these multiples is 23.
# Find the sum of all the multiples of 3 or 5 below 1000.

def multiple_3or5(n):
assert n > 0, 'Πρέπει να είναι μεγαλύτερος από 0.'
return True if not n % 3 or not n % 5 else False
sum = 0
for number in range(1, 1000):
if multiple_3or5(number):
sum += number
print(sum)


Problem 2: By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
Spoiler: show
Κώδικας: Επιλογή όλων
# Each new term in the Fibonacci sequence is generated by adding the previous two terms.
# By starting with 1 and 2, the first 10 terms will be:
# 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
# By considering the terms in the Fibonacci sequence whose values do not exceed four million,
# find the sum of the even-valued terms.

sum = 0
a, b = 0, 1
lista = [a, b]
while True:
a, b = b, a + b
if b >= 4000000:
break
lista.append(b)
for number in lista:
if not number % 2:
sum += number
print(sum)


Problem 3: Find the largest prime factor of a composite number.:
Spoiler: show
Κώδικας: Επιλογή όλων
# The prime factors of 13195 are 5, 7, 13 and 29.
# What is the largest prime factor of the number 600851475143 ?

def largest_prime_factor(n):
assert n > 1, 'Ο αριθμός πρέπει να είναι μεγαλύτερος του 1.'
while True:
a = False
for number in range(2, n):
if not n % number :
n /= number
n = int(n)
a = True
break
if a == False: # Το while σταματάει όταν το a = False, δηλαδή όταν ο n δεν μπορεί να έχει τέλεια διαίρεση
break # με κανέναν αριθμό.
return n
print(largest_prime_factor(600851475143))


Problem 4: Find the largest palindrome made from the product of two 3-digit numbers.
Spoiler: show
Κώδικας: Επιλογή όλων
# A palindromic number reads the same both ways.
# The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
# Find the largest palindrome made from the product of two 3-digit numbers.

def is_palindrome(string):
assert isinstance(string, str) or isinstance(string, int) or isinstance(string, float), 'Ακατάλληλος τύπος.'
string = str(string)
reverse = string[::-1]
return True if string == reverse else False
palindroma = []
for a in range(100, 1000):
for b in range(100, 1000):
c = a * b
if is_palindrome(c) and not c in palindroma:
palindroma.append(c)
print(max(palindroma))


Problem 5: What is the smallest number divisible by each of the numbers 1 to 20?:
Spoiler: show
Κώδικας: Επιλογή όλων
# 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
# What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?

def evenly_divisible_from_1_to_n(n):
assert n > 1, 'Πρέπει να είναι μεγαλύτερος του 1'
i = 1
for a in range(1, n + 1):
if i % a:
for b in range(1, n + 1):
if not (i * b) % a:
i *= b
break
return i
print(evenly_divisible_from_1_to_n(20))


Problem 6: What is the difference between the sum of the squares and the square of the sums?:
Spoiler: show
Κώδικας: Επιλογή όλων
# The sum of the squares of the first ten natural numbers is, 12 + 22 + ... + 102 = 385
# The square of the sum of the first ten natural numbers is, (1 + 2 + ... + 10)2 = 552 = 3025
# Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 − 385 = 2640.
# Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

def sum_of_the_squares(n):
assert n > 0, 'Πρέπει να είναι μεγαλύτερο του 1.'
sum = 0
for i in range(1, n + 1):
i **= 2
sum += i
return sum
def square_of_the_sum(n):
assert n > 0, 'Πρέπει να είναι μεγαλύτερο του 1.'
sum = 0
for i in range(1, n + 1):
sum += i
square = sum ** 2
return square
difference = square_of_the_sum(100) - sum_of_the_squares(100)
print(difference)