From fbee54a85a60c75926787d228c9a6db51427b828 Mon Sep 17 00:00:00 2001 From: Mahesh Tudu Date: Wed, 10 Oct 2018 03:49:31 +0530 Subject: [PATCH] Removed infinite loop from Nth magic no. --- .../Nth_magic_number/nth_magic_number.cpp | 24 +++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/BitManipulation/Nth_magic_number/nth_magic_number.cpp b/BitManipulation/Nth_magic_number/nth_magic_number.cpp index a3cc2648..1f107c90 100644 --- a/BitManipulation/Nth_magic_number/nth_magic_number.cpp +++ b/BitManipulation/Nth_magic_number/nth_magic_number.cpp @@ -1,25 +1,29 @@ #include using namespace std; - + int nthMagicNo(int n) { int pow = 1, answer = 0; - + + while (n) { - pow = pow*5; - - if (n & 1) - answer += pow; - + pow = pow*5; + + + if (n & 1) + answer += pow; + + + n >>= 1; } return answer; } - + int main() { int n; - cin>>n; + cin>>n; cout << "nth magic number is " << nthMagicNo(n) << endl; return 0; -} \ No newline at end of file +}