Skip to content

Instantly share code, notes, and snippets.

@Denenberg
Denenberg / В каждом целом числе от M до N включительно берётся последняя (младшая) цифра, и все такие цифры складываются. Найдите получившуюся сумму.
Created February 8, 2023 21:23
В каждом целом числе от M до N включительно берётся последняя (младшая) цифра, и все такие цифры складываются. Найдите получившуюся сумму.
# Задача 1. Последние цифры (100 баллов)
# ограничение по времени на тест: 1 секунда
# ограничение по памяти на тест: 256 мегабайт
# ввод / вывод: стандартный
# В каждом целом числе от M до N включительно
# берётся последняя (младшая) цифра,
# и все такие цифры складываются.
# Найдите получившуюся сумму.
def last_digit_sum(m, n):
@Denenberg
Denenberg / Трёхзначное простое число называется крутым, если последняя его цифра равна модулю разности первых двух. Напиши программу на Питоне, печатающую все крутые числа.
Created February 8, 2023 08:20
Трёхзначное простое число называется крутым, если последняя его цифра равна модулю разности первых двух. Напиши программу на Питоне, печатающую все крутые числа.
# Трёхзначное простое число называется крутым,
# если последняя его цифра равна модулю разности первых двух.
# Напиши программу на Питоне, печатающую все крутые числа.
def is_prime(n):
if n < 2:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
@Denenberg
Denenberg / Here's a simple Python program that prints the first n numbers each of them is a product of 1 distinct primes:
Created February 8, 2023 08:00
Here's a simple Python program that prints the first n numbers each of them is a product of 1 distinct primes:
# Here's a simple Python program that prints the first n numbers
# each of them is a product of 1 distinct primes:
# This program uses a simple algorithm to generate
# prime numbers up to a certain limit (n), and then prints the
# first n prime numbers.
def get_primes(n):
primes = []
candidate = 2
@Denenberg
Denenberg / Как вариант, вот алгоритм для поиска возрастающей подследовательности максимальной длины в списке:
Created February 8, 2023 07:41
Как вариант, вот алгоритм для поиска возрастающей подследовательности максимальной длины в списке:
# Как вариант, вот алгоритм для поиска возрастающей
# подследовательности максимальной длины в списке:
# Здесь используется динамическое программирование.
# Массив dp хранит длины возрастающих подследовательностей,
# заканчивающихся на текущем элементе.
# Для каждой пары элементов numbers[i] и numbers[j] сравнивается,
# является ли numbers[j] предшественником numbers[i]
# в возрастающей подследовательности. Если numbers[j] < numbers[i],
# то dp[i] обновляется с учетом длины возрастающей
@Denenberg
Denenberg / This game generates a random problem in the form of an equation like x + 5 = 12 and asks the user to solve for the variable. The user inputs their answer and the game checks if it's correct, printing a message accordingly.
Last active February 7, 2023 23:09
This game generates a random problem in the form of an equation like x + 5 = 12 and asks the user to solve for the variable. The user inputs their answer and the game checks if it's correct, printing a message accordingly.
# This game generates a random problem in the form of an
# equation like x + 5 = 12 and asks the user to solve for
# the variable. The user inputs their answer and the game
# checks if it's correct, printing a message accordingly.
import random
def generate_problem():
operations = ['+', '-', '*', '/']
operation = random.choice(operations)
@Denenberg
Denenberg / This game generates a random mathematical quiz, asks the user to solve it, and checks if the answer is correct. The user has unlimited attempts to solve the quiz. If the answer is correct, the user passes the quiz, otherwise the user fails.
Created February 7, 2023 22:46
This game generates a random mathematical quiz, asks the user to solve it, and checks if the answer is correct. The user has unlimited attempts to solve the quiz. If the answer is correct, the user passes the quiz, otherwise the user fails.
# This game generates a random mathematical quiz,
# asks the user to solve it, and checks if the answer
# is correct. The user has unlimited attempts to solve
# the quiz. If the answer is correct, the user passes
# the quiz, otherwise the user fails.
import random
def generate_quiz():
# generate two random numbers
@Denenberg
Denenberg / Последовательность A349597 задаётся по следующему правилу: a(n) is the sum of digits of a(n-1)! with a(1) = 3.
Last active February 7, 2023 00:08
Последовательность A349597 задаётся по следующему правилу: a(n) is the sum of digits of a(n-1)! with a(1) = 3.
# Последовательность A349597 задаётся по следующему правилу:
# a(n) is the sum of digits of a(n-1)! with a(1) = 3.
import sys
def factorial(n):
result = 1
for i in range(2, n + 1):
result *= i
@Denenberg
Denenberg / Here's a simple program that prints the product of the digits of M^N for all M and N in the given range: Here's an updated version of the program that sorts the results in ascending order:
Created February 6, 2023 13:49
Here's a simple program that prints the product of the digits of M^N for all M and N in the given range: Here's an updated version of the program that sorts the results in ascending order:
# Here's a simple program that prints the product
# of the digits of M^N for all M and N in the given range:
# Here's an updated version of the program
# that sorts the results in ascending order:
def product_of_digits(n):
result = 1
while n > 0:
result *= n % 10
@Denenberg
Denenberg / Here's a simple program that prints the product of the digits of M^N for all M and N in the given range:
Created February 6, 2023 13:28
Here's a simple program that prints the product of the digits of M^N for all M and N in the given range:
# Here's a simple program that prints the product
# of the digits of M^N for all M and N in the given range:
def product_of_digits(n):
result = 1
while n > 0:
result *= n % 10
n //= 10
return result
@Denenberg
Denenberg / Please, write a Python program that prints the first 5 numbers each of them is a product of 5 distinct primes...
Created February 5, 2023 14:53
Please, write a Python program that prints the first 5 numbers each of them is a product of 5 distinct primes...
# Please, write a Python program that prints the first 5 numbers
# each of them is a product of 5 distinct primes...
from sympy import primerange
def find_5_primes_product(limit):
primes = list(primerange(2, limit))
products = []
for i in range(len(primes) - 4):
for j in range(i+1, len(primes) - 3):