From f1ec42c445115cca258f0ade920984e3bd5fb820 Mon Sep 17 00:00:00 2001 From: SufyanKhan0 <67229679+SufyanKhan0@users.noreply.github.com> Date: Mon, 30 Aug 2021 04:38:07 +0530 Subject: [PATCH 1/4] On branch AbundantNumber Changes to be committed: new file: Python/abundantnumber.py --- Python/abundantnumber.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 Python/abundantnumber.py diff --git a/Python/abundantnumber.py b/Python/abundantnumber.py new file mode 100644 index 00000000..7e7f25b9 --- /dev/null +++ b/Python/abundantnumber.py @@ -0,0 +1,38 @@ +import math + +def getSum(n) : + sum = 0 + + + i = 1 + while i <= (math.sqrt(n)) : + if n%i == 0 : + + + if n/i == i : + sum = sum + i + else : + sum = sum + i + sum = sum + (n / i ) + i = i + 1 + + sum = sum - n + return sum + +def checkAbundant(n) : + + + if (getSum(n) > n) : + return 1 + else : + return 0 + +if(checkAbundant(12) == 1) : + print "YES" +else : + print "NO" + +if(checkAbundant(15) == 1) : + print "YES" +else : + print "NO" From 0f8462d5f4a0e21b6cf3b08e5293ae20a7f22bfc Mon Sep 17 00:00:00 2001 From: SufyanKhan0 <67229679+SufyanKhan0@users.noreply.github.com> Date: Tue, 31 Aug 2021 03:48:21 +0530 Subject: [PATCH 2/4] On branch HappyNumberPython Changes to be committed: new file: Python/HappyNumber.py --- Python/HappyNumber.py | 46 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Python/HappyNumber.py diff --git a/Python/HappyNumber.py b/Python/HappyNumber.py new file mode 100644 index 00000000..0ed6356e --- /dev/null +++ b/Python/HappyNumber.py @@ -0,0 +1,46 @@ +# Python program to check if a number is happy or not: + +# funtion to get sum of squares of the digits of a number: +def ssd(n): + a = b = 0; + while(n > 0): + a = n%10 + b += a*a + n //= 10 + return b + +# User input: +print("Enter Number to Check:") +n = int(input()) +h = n; + +while(h != 1 and h != 4): + h = ssd(h) + +if(h == 1): + print(n,"is a happy number") +elif(h == 4): + print(n,"is not a happy number") + +''' +Explanation: +A happy number is a number which eventually reaches 1 when replaced +by the sum of the square of each digit. All Non happy numbers eventually +become 4 and get stuck in an infinite cycle, therefore if a number reaches 4 +anytime in the cycle it is not a happy number + +Examples of Happy Numbers: +-> 100 = 1*1 + 0*0 +0*0 = 1 +-> 13 = 1*1 + 3*3 = 10, 10 = 1*1 + 0*0 = 1 +-> 19 = 1*1 + 9*9 = 82, 82 = 8*8 + 2*2 = 68, 68 = 6*6 + 8*8 = 100, 100 = 1*1 + 0*0 + 0*0 =1 + +Sample Input: +Enter Number to Check: +13 + +Sample Output: +13 is a happy number + +Time Complexity : O(log N) +Space Complexity : O(log N) +''' From 5e91b6e915f8bf20ffdfff560d558b648e818634 Mon Sep 17 00:00:00 2001 From: SufyanKhan0 <67229679+SufyanKhan0@users.noreply.github.com> Date: Tue, 31 Aug 2021 03:53:27 +0530 Subject: [PATCH 3/4] On branch HappyNumberPython Changes to be committed: modified: Python/README.md --- Python/README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Python/README.md b/Python/README.md index 19479a04..dbeccb06 100644 --- a/Python/README.md +++ b/Python/README.md @@ -66,6 +66,8 @@ Format: -[Program name](name of the file) -[Greatest Common divisor](gcd.py) +-[Happy Number](HappyNumber.py) + -[Heap Sort](Heap_sort.py) -[Inorder Tree Traversal](inordder.py) @@ -171,4 +173,3 @@ Format: -[Program name](name of the file) -[Unique Number From A Set of Numbers repeated twice except unique number](unique_number_repeated_twice_except_unique_number.py) -[All Hamiltonian Cycles/Paths](all_hamiltonian_cycles.py) - From 41211898cfca045254f65e763030cd72723ba4d4 Mon Sep 17 00:00:00 2001 From: SufyanKhan0 <67229679+SufyanKhan0@users.noreply.github.com> Date: Tue, 31 Aug 2021 03:57:56 +0530 Subject: [PATCH 4/4] On branch HappyNumberPython Changes to be committed: deleted: Python/abundantnumber.py --- Python/abundantnumber.py | 38 -------------------------------------- 1 file changed, 38 deletions(-) delete mode 100644 Python/abundantnumber.py diff --git a/Python/abundantnumber.py b/Python/abundantnumber.py deleted file mode 100644 index 7e7f25b9..00000000 --- a/Python/abundantnumber.py +++ /dev/null @@ -1,38 +0,0 @@ -import math - -def getSum(n) : - sum = 0 - - - i = 1 - while i <= (math.sqrt(n)) : - if n%i == 0 : - - - if n/i == i : - sum = sum + i - else : - sum = sum + i - sum = sum + (n / i ) - i = i + 1 - - sum = sum - n - return sum - -def checkAbundant(n) : - - - if (getSum(n) > n) : - return 1 - else : - return 0 - -if(checkAbundant(12) == 1) : - print "YES" -else : - print "NO" - -if(checkAbundant(15) == 1) : - print "YES" -else : - print "NO"