Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 77d5d15

Browse files
Update solutions4.py
1 parent 4f71d32 commit 77d5d15

File tree

1 file changed

+31
-2
lines changed
  • Problem Solving in Python/01_Basic Problems/Solutions

1 file changed

+31
-2
lines changed

‎Problem Solving in Python/01_Basic Problems/Solutions/solutions4.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
1+
# Armstrong number: Approach 1
2+
number = int(input("Enter the number: "))
3+
original_number = number
4+
sum_of_powers = 0
5+
exponent = 0
6+
7+
# To find the digits in the number
8+
temp = number
9+
while(temp > 0):
10+
temp //= 10
11+
exponent += 1
12+
13+
# Lets perform the sum of the powers
14+
temp = number
15+
while(temp > 0):
16+
digit = temp % 10
17+
sum_of_powers += digit ** exponent
18+
temp //= 10
19+
20+
# Conditional Setup
21+
if(sum_of_powers == original_number):
22+
print("This number is Armstrong Number")
23+
else:
24+
print("This number is not an Armstrong Number")
25+
26+
27+
# Armstrong number: Approach 2
28+
"""
129
number = int(input("Enter the number: "))
230
numStr = str(number)
331
exponent = len(numStr)
@@ -8,6 +36,7 @@
836
print("This number is Armstrong Number")
937
else:
1038
print("This number is not an Armstrong Number")
11-
39+
"""
40+
# Time complexity for Second Approach
1241
# Complexity: O(logn)
13-
# Space Complexity: O(1) # because we are using a constant amount of space.
42+
# Space Complexity: O(1) # because we are using a constant amount of space.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /