A positive integer is called vaivém when, considering its representation in base ten, the first digit from left to right is greater than the second, the second is less than the third, the third is bigger than the fourth and so on alternating bigger and smaller until the last digit. For example, 2021 is vaivém, as 2 > 0 and 0 < 2 and 2 > 1. The number 2023 is not vaivém, as 2 > 0 and 0 < 2, but 2 is not greater than 3.
a) How many vaivém positive integers are there from 2000 to 2100?
b) What is the largest vaivém number without repeating digits?
c) How many distinct 7-digit numbers formed by all the digits 1, 2, 3, 4, 5, 6 and 7 are vaivém?
a) 45
b) 9785634120 Found by logic, confirmed by computer
c) 272
The vaivém numbers between 2000 to 2100 are:
[2010, 2020, 2021, 2030, 2031, 2032, 2040, 2041, 2042, 2043, 2050, 2051, 2052, 2053, 2054, 2060, 2061, 2062, 2063, 2064, 2065, 2070, 2071, 2072, 2073, 2074, 2075, 2076, 2080, 2081, 2082, 2083, 2084, 2085, 2086, 2087, 2090, 2091, 2092, 2093, 2094, 2095, 2096, 2097, 2098]
------------
def isSaw(n):
""" mountain digits that go down then up, alternating """
s_list = [int(a) for a in list(str(n))]
for i,v in enumerate(s_list):
if i == 0:
continue
if i%2 == 1:
if v >= s_list[i-1]:
return False
elif i%2 == 0:
if v <= s_list[i-1]:
return False
return True
# a)
lista = []
count = 0
for n in range(2000,2101):
if isSaw(n):
count += 1
lista.append(n)
print(count)
# b)
from itertools import permutations
for p in permutations('9876543210'):
n = int(''.join(p))
if isSaw(n):
print(n)
break
# c)
count2 = 0
for p in permutations('1234567'):
n = int(''.join(p))
if isSaw(n):
count2 += 1
print(count2)
print('\n', 'The vaivém numbers between 2000 to 2100 are:', '\n')
print(lista)
|
Posted by Larry
on 2024-07-13 10:13:26 |