File tree 1 file changed +113
-37
lines changed
1 file changed +113
-37
lines changed Original file line number Diff line number Diff line change 1
- def isHappy (n ):
2
- sum = 0
3
- isHapp = True
4
- while (n > 0 ):
5
- sum += (n % 10 )* (n % 10 )
6
- # print(sum)
7
- n = n // 10
8
- if sum == 1 :
9
- return isHapp
10
-
11
- elif sum == 4 :
12
- isHapp = False
13
- return isHapp
14
- break
15
-
16
- isHappy (sum )
17
-
18
-
19
- # num = 11
20
-
21
- #
22
- # def isHappyNumber(num):
1
+ # def isHappy(n):
23
2
# sum = 0
24
3
#
25
- # while (num > 0):
26
- # rem = num % 10
27
- # sum = sum + (rem * rem)
28
- # num = num // 10
4
+ # while(n>0):
5
+ # sum += (n%10)*(n%10)
6
+ # # print(sum)
7
+ # n = n//10
8
+ # if sum == 1:
9
+ # return True
10
+ #
11
+ # elif sum == 4:
12
+ # return False
29
13
#
14
+ # return isHappy(sum)
30
15
#
31
- # while (sum != 1 and sum != 4):
32
- # sum = isHappyNumber(sum)
33
16
#
34
- # if (sum == 1):
35
- # return True
36
17
#
37
- # elif (sum == 4):
38
- # return False
18
+ #
19
+ # # num = 11
20
+ #
21
+ # #
22
+ # # def isHappyNumber(num):
23
+ # # sum = 0
24
+ # #
25
+ # # while (num > 0):
26
+ # # rem = num % 10
27
+ # # sum = sum + (rem * rem)
28
+ # # num = num // 10
29
+ # #
30
+ # #
31
+ # # while (sum != 1 and sum != 4):
32
+ # # sum = isHappyNumber(sum)
33
+ # #
34
+ # # if (sum == 1):
35
+ # # return True
36
+ # #
37
+ # # elif (sum == 4):
38
+ # # return False
39
+ #
40
+ #
41
+ #
42
+ # def isPrime(n):
43
+ # i = 2
44
+ # isPrime = True
45
+ # if n == 1:
46
+ # isPrime =False
47
+ # while i * i <= n:
48
+ # if n % i == 0:
49
+ # isPrime = False
50
+ # break
51
+ # i+=1
52
+ #
53
+ #
54
+ # return isPrime
55
+ #
56
+ #
57
+ # # print(isPrime(int(input())))
58
+ # if __name__ == '__main__':
59
+ #
60
+ # x = int(input())
61
+ # y = int(input())
62
+ # n = int(input())
63
+ # if x <= y and x > 0 and y >0:
64
+ #
65
+ # count = 1
66
+ #
67
+ # for i in range(x,y):
68
+ #
69
+ # if isHappy(i)==isPrime(i)==True:
70
+ #
71
+ # if count ==n:
72
+ # print(i)
73
+ # count += 1
74
+ # else:
75
+ # print("No number is present at this index")
76
+ # else:
77
+ # print("Invalid Input")
78
+ #
79
+ # # print("No number found")
80
+ def isHappy (n ):
81
+ sum = 0
82
+ while (n > 0 ):
83
+ sum += (n % 10 ) * (n % 10 )
84
+ n = n // 10
85
+ if sum == 1 :
86
+ return True
39
87
88
+ elif sum == 4 :
89
+ return False
90
+
91
+ return isHappy (sum )
40
92
41
93
42
94
def isPrime (n ):
43
95
i = 2
44
96
isPrime = True
97
+ if n == 1 :
98
+ isPrime = False
45
99
while i * i <= n :
46
100
if n % i == 0 :
47
101
isPrime = False
48
102
break
49
- i += 1
103
+ i += 1
104
+ return isPrime
50
105
51
106
52
- return isPrime
107
+ if __name__ == '__main__' :
108
+
109
+ try :
110
+ x = int (input ())
111
+ y = int (input ())
112
+ n = int (input ())
113
+
114
+ if x <= y and x > 0 and n > 0 :
115
+
116
+ count = 0
117
+
118
+ for i in range (x , y + 1 ):
119
+
120
+ if isHappy (i ) == isPrime (i ) == True :
121
+ count += 1
122
+ if count == n :
123
+ print (i )
124
+
125
+ elif count == 0 :
126
+ print ("No number is present at this index" )
127
+ else :
128
+ print ("Invalid Input" )
129
+
130
+ except ValueError :
131
+ print ("Invalid Input" )
53
132
54
133
55
- # print(isPrime(int(input())))
56
- print (isHappy (17 ))
57
- print (isPrime (15 ))
You can’t perform that action at this time.
0 commit comments