-
Notifications
You must be signed in to change notification settings - Fork 3
/
70_Microsoft_Perfect_Numbers_Sum_to_10.py
executable file
·79 lines (59 loc) · 2.01 KB
/
70_Microsoft_Perfect_Numbers_Sum_to_10.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
"""
This problem was asked by Microsoft.
A number is considered perfect if its digits sum up to exactly 10.
Given a positive integer n, return the n-th perfect number.
For example, given 1, you should return 19. Given 2, you should return 28.
"""
# the first perfect num is 19
# the second perfect num is 28
# the third +ive num is 37
# closer look reveals:
# n=1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
# -----------------------------------------------
# 19, 28, 37 46, 64, 46, 73, 82, 91, 100, 109, 118
# so 19+(n*9)
# but there is one problem in this method 10th->100 number if not a perfect num
# 10th number should be the next one then 10th=>109
# so we'll also need to push 11th num to be the 12th num and so on
# note that the outliers of this method exist in form of {100, 199, 299,1000, 10000,etc all nums that don't sum to 10}
# before we return the result we need to adjust for the outliers:
# the result should be
# n= 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
# -----------------------------------------------
# 19, 28, 37 46, 64, 46, 73, 82, 91, 109, 118, 127
def find_perfect_num(n):
if n ==0:
return 'nums start from 1'
n_th_num = 19 # start with 19
count = 0 # iterator
while(True):
curr_sum = sum(list(map(int, str(n_th_num))))
if curr_sum ==10:
count+=1
if count == n:
return n_th_num
n_th_num += 9
#
# def outliers(n): # experiment to see if there is a pattern to the outliers -> There isn't :(
# l=[]
#
# for i in range(1, n):
# num = ((i+1)*9)+1
#
# s = sum(list(map(int, str(num))))
#
# if s!=10:
# l.append(num)
#
# return l
if __name__ == '__main__':
# print(find_perfect_num(1))
# print(find_perfect_num(9))
# print(find_perfect_num(11))
# print(find_perfect_num(21))
# print(find_perfect_num(91))
# print(find_perfect_num(88))
#
for i in range(1, 101):
print("{}#: {}".format(i, find_perfect_num(i)))
# print(outliers(2001))