I'm trying to to find number that equals or is greater than N and divisible by 3 and only consists of 0 and 1
Here is my code:
for x in range(1, 11):
p = x**10 # power x by 10
print(x, p) # output number itself and power result
At this point I'm stuck: We need to find number equals or greater than N divisible by 3 consists only of only 0 and 1.
If i try:
while True:
try:
N = int(input('Enter number from 1 to 10: '))
except ValueError:
print("That's not a number!")
pass
else:
if 1 <= N < 10:
print('Everythig is OK, you entered number in 1 to 10 range')
# start variant 1
found = False
i = N
while not found:
if i % 3 == 0:
try:
dummy = int(str(i), 2)
print(i)
found = True
except:
pass
i += 1
# end variant 1
pass
else:
print('Out of range. Try again')
pass
result entering any number in range is 111 Same result returns:
import itertools
while True:
try:
N = int(input('Enter number from 1 to 10: '))
except ValueError:
print("That's not a number!")
pass
else:
if 1 <= N < 10:
print('Everythig is OK, you entered number in 1 to 10 range')
# start variant 2
ints = itertools.count(N + 1)
filtered = filter(lambda x: set(str(x)) <= {"0", "1"}, ints)
filtered = filter(lambda x: not x % 3, filtered)
result = next(filtered)
print(result)
# end variant 2
pass
else:
print('Out of range. Try again')
pass
if we change condition of numbers list if 1 <= N < 10 to if 1 <= N < 1010** both variants are correct.
P.s. if we enter e.g. 234234234 number algorithm almost died. Less numbers it founds faster )) Thank you for help!