Last active
June 4, 2023 08:48
-
-
Save Tuhin-thinks/1775ec9c00fd18a7c07ef174cdb6a8ea to your computer and use it in GitHub Desktop.
Problem statement: Next week's winning lottery numbers are 8 different numbers all in the range 11 to 99 inclusiveThree, and only three of them are prime numbers and the sum of these three is 265Of the other 5, just two of them are even, and when these 2 are multiplied together they make 1332The mean of the remaining 3 is 23What are the eight wi…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Problem statement: Next week's winning lottery numbers are 8 different numbers all in the range 11 to 99 inclusive | |
Three, and only three of them are prime numbers and the sum of these three is 265 | |
Of the other 5, just two of them are even, and when these 2 are multiplied together they make 1332 | |
The mean of the remaining 3 is 23 | |
What are the eight winning lottery numbers? | |
""" | |
# (x + y + z) == 265 and all((is_prime(num) for num in (x, y, z))) | |
# all((is_even(num) for num in (p, q))) and p * q == 1332 | |
# mean((a, b, c)) == 23 | |
from typing import Iterable | |
from itertools import combinations | |
from statistics import mean | |
import math | |
def is_prime(number): | |
if number < 2: | |
return False | |
if number == 2: | |
return True | |
for num in range(2, int(math.sqrt(number))+1): | |
if number % num == 0: | |
return False | |
return True | |
def is_even(number): | |
return number % 2 == 0 | |
def check_not_condition(numbers: Iterable[int]): | |
""" | |
Not condition ensures, that: | |
- only 3 numbers are prime | |
- only 2 numbers are even | |
:param numbers: | |
:return: | |
""" | |
prime_count = [1 for num in numbers if is_prime(num)].count(1) | |
even_count = [1 for num in numbers if is_even(num)].count(1) | |
return prime_count == 3 and even_count == 2 | |
def solve(): | |
# get all the combinations of 3 prime numbers | |
prime_numbers = [num for num in range(11, 100) if is_prime(num)] | |
even_numbers = [num for num in range(11, 100) if is_even(num)] | |
prime_combinations = combinations(prime_numbers, 3) | |
_possible_prime_numbers = [] | |
for x, y, z in prime_combinations: | |
if (x + y + z) == 265: | |
_possible_prime_numbers.append((x, y, z)) | |
_possible_five_numbers = [] | |
for x, y, z in _possible_prime_numbers: | |
# get all the combinations of 2 even numbers | |
even_combinations = combinations(even_numbers, 2) | |
for p, q in even_combinations: | |
if all((is_even(num) for num in (p, q))) and p * q == 1332: | |
_possible_five_numbers.append((x, y, z, p, q)) | |
for x, y, z, p, q in _possible_five_numbers: | |
# get all the combinations of 3 numbers whose mean is 23 | |
remaining_numbers = [num for num in range(11, 100) if num not in (x, y, z, p, q)] | |
remaining_combinations = combinations(remaining_numbers, 3) | |
for a, b, c in remaining_combinations: | |
if mean((a, b, c)) == 23 and check_not_condition((x, y, z, p, q, a, b, c)): | |
return x, y, z, p, q, a, b, c | |
if __name__ == '__main__': | |
res = solve() | |
print(res) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment