Created
March 9, 2022 19:20
-
-
Save C10udburst/7c6c74d87253ea206e77d1d6b6594027 to your computer and use it in GitHub Desktop.
This file contains 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
from functools import reduce | |
from math import sqrt | |
numbers_str = open("liczby7.txt", "r", encoding='utf-8').read().strip().split("\n") | |
numbers = [int(x) for x in numbers_str] | |
max_number = reduce(max, numbers) | |
prime_list_max = max_number + 1000 # musi byc wiecej aby is_prime[i + 2] zadzialalo dla duzych liczb | |
is_prime = [True] * prime_list_max | |
is_prime[0] = False | |
is_prime[1] = False | |
for num in range(2, max_number + 1): | |
if not is_prime[num]: | |
continue | |
i = num * num | |
while i < prime_list_max: | |
is_prime[i] = False | |
i += num | |
print("Zadanie 1.") | |
sum = 0 | |
for num in numbers: | |
sq = int(sqrt(num)) | |
if sq * sq != num: | |
continue | |
if is_prime[sq]: | |
sum += 1 | |
print(f"Ilosc: {sum}") | |
print() | |
print("Zadanie 2.") | |
sum = 0 | |
for num in numbers: | |
i = num | |
while not (is_prime[i] and is_prime[i + 2]): | |
i += 1 | |
sum += i + (i + 2) | |
print(f"Suma: {sum}") | |
print() | |
print("Zadanie 3.") | |
anagrams = dict() | |
for num in numbers_str: | |
key = list(num) | |
key.sort() | |
key = "".join(key) | |
anagrams[key] = anagrams.get(key, 0) + 1 | |
sum = 0 | |
for c in anagrams.values(): | |
if c > 1: | |
sum += c | |
print(f"Ilosc: {sum}") | |
print() | |
del anagrams | |
print("Zadanie 4.") | |
number = reduce(lambda x, y: x + y, numbers_str[:1000]) + "1" | |
even_chunk = "" | |
start = int(number[0]) % 2 | |
for i in range(1, len(number)): | |
if int(number[i]) % 2 == 1: | |
new_chunk = number[start:i] | |
if len(new_chunk) > len(even_chunk): | |
even_chunk = new_chunk | |
start = i + 1 | |
print(f"Ciag: \"{even_chunk}\", dlugosc: {len(even_chunk)} cyfr") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment