Задача

Определить, является ли введеный год високосным. Год является високосным, если его номер кратен 4, но не кратен 100, а также если он кратен 400

In [29]:
def is_number(str):
    try:
        int(str)
        return True
    except:
        return False

isnumber = False

while not isnumber:
    year = input('Введите год: ')
    isnumber = is_number(year)
    if isnumber:
        if (int(year) % 4 == 0  and int(year) % 100 != 0) or int(year) % 400 == 0:
            print ('Год високосный')
        else: 
            print ('Год не високосный')
        break
    else:
        print ('Вы ввели некорректное значение')
        continue


Введите год: 2000
Год високосный

FizzBuzz

Напишите программу, которая выводит на экран числа от 1 до 100. При этом вместо чисел, кратных трем, программа должна выводить слово Fizz, а вместо чисел, кратных пяти — слово Buzz. Если число кратно пятнадцати, то программа должна выводить слово FizzBuzz.

In [36]:
for i in range (1, 101):
    if i % 3 == 0:
        if i % 5 == 0:
            print (i, 'FizzBuzz')
            continue
        print (i, 'Fizz')
    if i % 5 == 0:
        print (i, 'Buzz')


3 Fizz
5 Buzz
6 Fizz
9 Fizz
10 Buzz
12 Fizz
15 FizzBuzz
18 Fizz
20 Buzz
21 Fizz
24 Fizz
25 Buzz
27 Fizz
30 FizzBuzz
33 Fizz
35 Buzz
36 Fizz
39 Fizz
40 Buzz
42 Fizz
45 FizzBuzz
48 Fizz
50 Buzz
51 Fizz
54 Fizz
55 Buzz
57 Fizz
60 FizzBuzz
63 Fizz
65 Buzz
66 Fizz
69 Fizz
70 Buzz
72 Fizz
75 FizzBuzz
78 Fizz
80 Buzz
81 Fizz
84 Fizz
85 Buzz
87 Fizz
90 FizzBuzz
93 Fizz
95 Buzz
96 Fizz
99 Fizz
100 Buzz

Problem 1

Multiples of 3 and 5

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.

In [5]:
s = 0
a = 0
arr = []
x = 999
while s < x:
    s += 1
    if s % 3 == 0 or s % 5 == 0:
        arr.append(s)
        a += s
        continue
print (a)


233168

Problem 2

Even Fibonacci numbers

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.

In [8]:
s = 0
i = 0
c = []
a = [1, 2]
d = 4000000
x = True
while x:
    s = a[i] + a[i + 1]
    if s > d:
        x = False
        break
    a.append(s)
    i += 1 
    continue
for e in range(len(a)):
    if a[e] % 2 == 0:
        c.append(a[e])
print (sum(c), a, c)


4613732 [1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578] [2, 8, 34, 144, 610, 2584, 10946, 46368, 196418, 832040, 3524578]

Problem 3

Largest prime factor

The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?

In [9]:
maxF = 600851475143
i = 2
p = 1
PrimeFacrors = []
while i < maxF:
    if maxF % i == 0:
        p *= i
        PrimeFacrors.append(i)
        if p == maxF:
            break
    i += 1
    continue
print (max(PrimeFacrors), p)


6857 600851475143

Problem 4

Largest palindrome product

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.

In [44]:
def palindrome(number):
    return str(number) == str(number)[::-1]

MIN = 100
MAX = 1000

def largest():
    max_number = 0
    for i in range(MIN, MAX):
        for j in range(MIN, MAX):
            if palindrome(i*j) and i*j > max_number:
                max_number = i*j
    return max_number

print (largest())


906609

Problem 5

Smallest multiple

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?
Ниже приведены два решения данной задачи, первое в лоб) (простой перебор и время расчета около 100 секунд), второе не успела довести до ума.

In [18]:
import timeit

start = timeit.default_timer()

num = 1
i = 1
while i < 19:
    for j in range(1, 21):
        if num % j == 0:
            i += 1
            continue
        else:
            num += 1
            i = 1
            
stop = timeit.default_timer()

print (num, "Time: ", start - stop)


232792560 Time:  -87.2823321330361

In [15]:
import timeit

start = timeit.default_timer()

i = 20
set_num = set()
range_num = list()
counter = 0

while i > 2:
    for j in range (1, 21):
        if i % j == 0:
            range_num.append(j)
            counter += 1
        continue
        
    if counter > 2:
        for k in range_num:
            if (k != i):
                set_num.add(k)
    else:
        set_num.add(i)
        
    range_num = []
    counter = 0
    i -= 1

counter = 1
for m in set_num:
    counter = counter * m
    
stop = timeit.default_timer()

print (set_num, "Number: ", counter / 10 / 9 / 8, "Time: ", stop - start)


{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 17, 19} Number:  232792560.0 Time:  0.0007343549805227667