From 6997cbb9b485ccc8c04562eebbdbc4026eb27548 Mon Sep 17 00:00:00 2001 From: Hrishab Date: Mon, 13 Jan 2025 23:17:47 +0530 Subject: [PATCH] imporoved the code by reducing the amount of time to calculate factorial --- strong.py | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/strong.py b/strong.py index 13cf2dc5..273913db 100644 --- a/strong.py +++ b/strong.py @@ -1,16 +1,14 @@ -sum1=0 -num=int(input("Enter a number:")) -temp=num -while(num): - i=1 - f=1 - r=num%10 - while(i<=r): - f=f*i - i=i+1 - sum1=sum1+f - num=num//10 -if(sum1==temp): +factorials = [1] * 10 +for i in range(2, 10): + factorials[i] = factorials[i-1] * i +sum1 = 0 +num = int(input("Enter a number:")) +temp = num +while num: + r = num % 10 + sum1 += factorials[r] + num //= 10 +if sum1 == temp: print("The number is a strong number") else: - print("The number is not a strong number") + print("The number is not a strong number") \ No newline at end of file