From 131b3b205fe1e6e2808b90db241a6be6124d1cf0 Mon Sep 17 00:00:00 2001 From: Slowlemon1423 Date: Sun, 8 Dec 2024 22:38:43 -0800 Subject: [PATCH 1/2] Optimised Happy Number and Simplified Factorial Recursion. BubbleSort.py in DSApython case confliction solved --- .../Sorting Algo/{BubbleSort.py => BubbleSort_case2.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename DSA/DSA-python/Sorting Algo/{BubbleSort.py => BubbleSort_case2.py} (100%) diff --git a/DSA/DSA-python/Sorting Algo/BubbleSort.py b/DSA/DSA-python/Sorting Algo/BubbleSort_case2.py similarity index 100% rename from DSA/DSA-python/Sorting Algo/BubbleSort.py rename to DSA/DSA-python/Sorting Algo/BubbleSort_case2.py From eadee8581c8d48ad337003e0658c00ce6f4192bf Mon Sep 17 00:00:00 2001 From: Slowlemon1423 Date: Sun, 8 Dec 2024 22:56:20 -0800 Subject: [PATCH 2/2] Optimized factorial recursion and happy number logic --- C++/factorial_recursion.cpp | 18 ++++-- C++/happy number.cpp | 62 +++++++++++++------ .../Sorting Algo/BubbleSort_case2.py | 20 ------ 3 files changed, 57 insertions(+), 43 deletions(-) delete mode 100644 DSA/DSA-python/Sorting Algo/BubbleSort_case2.py diff --git a/C++/factorial_recursion.cpp b/C++/factorial_recursion.cpp index 0129dbe0..ae84a435 100644 --- a/C++/factorial_recursion.cpp +++ b/C++/factorial_recursion.cpp @@ -1,10 +1,20 @@ #include using namespace std; -int factorial(int n) { - - // single line to find factorial - return (n == 1 || n == 0) ? 1 : n * factorial(n - 1); +long factorial(int n) { + if(n==1){ + return 1; //1! is 1 + } + if(n==0){ + return 1; //0! is 1 + } + return n * factorial(n-1); + /* + n! is the same as n * (n-1)! + n! equals n * (n-1) * (n-2)... 3 * 2 * 1 + (n-1)! equals (n-1) * (n-2)... 3 * 2 * 1 + (n-1)! * n equals n! and also equal to n * (n-1) * (n-2)... 3 * 2 * 1 + */ } int main() { diff --git a/C++/happy number.cpp b/C++/happy number.cpp index f2a309a6..153f11e2 100644 --- a/C++/happy number.cpp +++ b/C++/happy number.cpp @@ -14,28 +14,52 @@ Example 1: #include using namespace std; -int find(int n) -{ - - int sum=0,r; - while(n!=0) - { - r=n%10; - sum=sum+(r*r); - n=n/10; + + +int Sods(int n){ // Finds Square Digital Sum + int Sum = 0; + while(n!=0){ + int mod = (n%10); + Sum += mod*mod; + n = n/10; } - - if ((sum==1)||(sum==7)) - { - return 1; - + return Sum; +} +bool find(int n) +{ + /* + Tortise Hare Explanation: + we have two variables Slow and Fast and a function Sods which calculates the sum of squares of digits of the input number. + Each Iteration, We Run fast through Sods twice and run Slow through Sods once. + If we make Fast update 2 times as fast as slow, then we will be able to find any cycle and if Fast is ever 1, then we know that the number is happy. + Example: + Test=2 + Fast | Slow + -----+----- + 16 | 4 + 58 | 16 + 145 | 37 + 20 | 58 + 16 | 89 + 58 | 145 + 145 | 42 + 20 | 20 + Since Fast is equal to Slow, 2 is an Unhappy Number + Cycle: 2→4→16→37→58→89→145→42→20→4→16... + */ + int Fast = Sods(Sods(n)); + int Slow = Sods(n); + if(Fast == 1){ + return true; // Edge Case of n=1 which makes the while loop never happen but still is happy. } - else if((sum==2)||(sum==3)||(sum==4)||(sum==5)||(sum==6)||(sum==8)||(sum==9)) - { - return 0; + while(Fast != Slow){ + if(Fast == 1){ + return true; + } + Fast = Sods(Sods(Fast)); + Slow = Sods(Slow); } - else - find(sum); + return false; } int main() { diff --git a/DSA/DSA-python/Sorting Algo/BubbleSort_case2.py b/DSA/DSA-python/Sorting Algo/BubbleSort_case2.py deleted file mode 100644 index cd5dd192..00000000 --- a/DSA/DSA-python/Sorting Algo/BubbleSort_case2.py +++ /dev/null @@ -1,20 +0,0 @@ -#this is the most easiest algorith which jst involves swapping of elements -import array -def BubbleSort(a): - n = len(a) - - for i in range(n): - for j in range(i+1 , n): - if(a[i]>a[j]): - a[i] , a[j] = a[j] , a[i] - -a = array.array('i' , []) - -n = int(input("Enter lenght of array : ")) - -for i in range(n): - c = int(input("Enter the number : ")) - a.append(c) - -BubbleSort(a) -print(list(a)) \ No newline at end of file