Output:<\/strong><\/p>\nThe value of the power of base and exponent modular 10^9+7 = 895629451<\/pre>\nProgram for Exponential Squaring (Fast Modulo Multiplication) in Python<\/h2>\n Below are the ways to find the power of base and exponent modular 10^9+7 for the given base and exponential values:<\/p>\n
Approach:<\/strong><\/p>\n\nGive the base value as static input and store it in a variable.<\/li>\n Give the exponential value as static input and store it in another variable.<\/li>\n Pass the given base and exponential values as the arguments to the exponentl_squaring() <\/strong>function and store it in a variable.<\/li>\nTake a variable to say numb<\/strong> and initialize its value with 1000000007(10^9+7).<\/li>\nCreate a function to say exponentl_squaring() <\/strong>which takes the given two base and exponential values as the arguments and returns the value of the power of base and exponent modular 10^9+7.<\/li>\nInside the function, take a variable say p,<\/strong>\u00a0and initialize its value to 1.<\/li>\nLoop until the given exponential value is greater than 0 using the while loop.<\/li>\n Check if the given exponential value is odd using the if conditional statement.<\/li>\n If it is true, multiply p <\/strong>with the given base value and store it in another variable.<\/li>\nCalculate the value of the above result modulus numb(10^9+7)<\/strong> and store it in the same variable p.<\/li>\nMultiply the given base value with itself and apply the modulus operator with 10^9+7(numb<\/strong>).<\/li>\nStore it in the same variable given base value.<\/li>\n Divide the given exponential value by 2 and convert it to an integer using the int() function.<\/li>\n Store it in the same variable given exponential value.<\/li>\n Return the value of p modulus 10^9+7.<\/li>\n Print the value of the power of base and exponent modular 10^9+7.<\/li>\n The Exit of the Program.<\/li>\n<\/ul>\nBelow is the implementation:<\/strong><\/p>\n# Take a variable to say numb and initialize its value with 1000000007(10^9+7).\r\nnumb = 1000000007\r\n\r\n# Create a function to say exponentl_squaring() which takes the given two base and\r\n# exponential values as the arguments and returns the value of the power of base and\r\n# exponent modular 10^9+7.\r\n\r\n\r\ndef exponentl_squaring(gvn_baseval, gvn_exponentlval):\r\n # Inside the function, take a variable say p,\u00a0and initialize its value to 1.\r\n p = 1\r\n # Loop until the given exponential value is greater than 0 using the while loop.\r\n while(gvn_exponentlval > 0):\r\n # Check if the given exponential value is odd using the if conditional statement.\r\n if (gvn_exponentlval % 2 != 0):\r\n # If it is true, multiply p with the given base value and store it in another\r\n # variable.\r\n k = p * gvn_baseval\r\n # Calculate the value of the above result modulus numb(10^9+7) and store it in the\r\n # same variable p.\r\n p = k % numb\r\n # Multiply the given base value with itself and apply the modulus operator with\r\n # 10^9+7(numb).\r\n # Store it in the same variable given base value.\r\n gvn_baseval = (gvn_baseval * gvn_baseval) % numb\r\n # Divide the given exponential value by 2 and convert it to an integer using the\r\n # int() function.\r\n # Store it in the same variable given exponential value.\r\n gvn_exponentlval = int(gvn_exponentlval \/ 2)\r\n # Return the value of p modulus 10^9+7.\r\n return p % numb\r\n\r\n\r\n# Give the base value as static input and store it in a variable.\r\ngvn_baseval = 5\r\n# Give the exponential value as static input and store it in another variable.\r\ngvn_exponentlval = 3\r\n# Pass the given base and exponential values as the arguments to the exponentl_squaring()\r\n# function and store it in a variable.\r\nrslt = exponentl_squaring(gvn_baseval, gvn_exponentlval)\r\n# Print the value of the power of base and exponent modular 10^9+7.\r\nprint(\"The value of the power of base and exponent modular 10^9+7 = \", rslt)\r\n<\/pre>\nOutput:<\/strong><\/p>\nThe value of the power of base and exponent modular 10^9+7 = 125<\/pre>\nApproach:<\/strong><\/p>\n\nGive the base value as user input using the int(input()) function and store it in a variable.<\/li>\n Give the exponential value as user input using the int(input()) function and store it in another variable.<\/li>\n Pass the given base and exponential values as the arguments to the exponentl_squaring() <\/strong>function and store it in a variable.<\/li>\nTake a variable to say numb<\/strong> and initialize its value with 1000000007(10^9+7).<\/li>\nCreate a function to say exponentl_squaring() <\/strong>which takes the given two base and exponential values as the arguments and returns the value of the power of base and exponent modular 10^9+7.<\/li>\nInside the function, take a variable say p,<\/strong>\u00a0and initialize its value to 1.<\/li>\nLoop until the given exponential value is greater than 0 using the while loop.<\/li>\n Check if the given exponential value is odd using the if conditional statement.<\/li>\n If it is true, multiply p <\/strong>with the given base value and store it in another variable.<\/li>\nCalculate the value of the above result modulus numb(10^9+7)<\/strong> and store it in the same variable p.<\/li>\nMultiply the given base value with itself and apply the modulus operator with 10^9+7(numb<\/strong>).<\/li>\nStore it in the same variable given the base value.<\/li>\n Divide the given exponential value by 2 and convert it to an integer using the int() function.<\/li>\n Store it in the same variable given exponential value.<\/li>\n Return the value of p modulus 10^9+7.<\/li>\n Print the value of the power of base and exponent modular 10^9+7.<\/li>\n The Exit of the Program.<\/li>\n<\/ul>\nBelow is the implementation:<\/strong><\/p>\n# Take a variable to say numb and initialize its value with 1000000007(10^9+7).\r\nnumb = 1000000007\r\n\r\n# Create a function to say exponentl_squaring() which takes the given two base and\r\n# exponential values as the arguments and returns the value of the power of base and\r\n# exponent modular 10^9+7.\r\n\r\n\r\ndef exponentl_squaring(gvn_baseval, gvn_exponentlval):\r\n # Inside the function, take a variable say p,\u00a0and initialize its value to 1.\r\n p = 1\r\n # Loop until the given exponential value is greater than 0 using the while loop.\r\n while(gvn_exponentlval > 0):\r\n # Check if the given exponential value is odd using the if conditional statement.\r\n if (gvn_exponentlval % 2 != 0):\r\n # If it is true, multiply p with the given base value and store it in another\r\n # variable.\r\n k = p * gvn_baseval\r\n # Calculate the value of the above result modulus numb(10^9+7) and store it in the\r\n # same variable p.\r\n p = k % numb\r\n # Multiply the given base value with itself and apply the modulus operator with\r\n # 10^9+7(numb).\r\n # Store it in the same variable given base value.\r\n gvn_baseval = (gvn_baseval * gvn_baseval) % numb\r\n # Divide the given exponential value by 2 and convert it to an integer using the\r\n # int() function.\r\n # Store it in the same variable given exponential value.\r\n gvn_exponentlval = int(gvn_exponentlval \/ 2)\r\n # Return the value of p modulus 10^9+7.\r\n return p % numb\r\n\r\n# Give the base value as user input using the int(input()) function and store it in a variable.\r\ngvn_baseval = int(input(\"Enter some random number = \"))\r\n# Give the exponential value as user input using the int(input()) function and \r\n# store it in another variable.\r\ngvn_exponentlval = int(input(\"Enter some random number = \"))\r\n# Pass the given base and exponential values as the arguments to the exponentl_squaring()\r\n# function and store it in a variable.\r\nrslt = exponentl_squaring(gvn_baseval, gvn_exponentlval)\r\n# Print the value of the power of base and exponent modular 10^9+7.\r\nprint(\"The value of the power of base and exponent modular 10^9+7 = \", rslt)\r\n<\/pre>\nOutput:<\/strong><\/p>\nEnter some random number = 3\r\nEnter some random number = 10000\r\nThe value of the power of base and exponent modular 10^9+7 = 895629451<\/pre>\n","protected":false},"excerpt":{"rendered":"Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease. Given two numbers base value and the exponential value, the task is to find the power of base and exponent modular 10^9+7 Examples: Example1: Input: Given base value =\u00a0 …<\/p>\n
Python Program for Exponential Squaring (Fast Modulo Multiplication)<\/span> Read More »<\/a><\/p>\n","protected":false},"author":7,"featured_media":20813,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[5],"tags":[],"yoast_head":"\nPython Program for Exponential Squaring (Fast Modulo Multiplication) - Python Programs<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n \n \n \n \n\t \n\t \n\t \n