{"id":20702,"date":"2021-09-21T09:00:06","date_gmt":"2021-09-21T03:30:06","guid":{"rendered":"https:\/\/python-programs.com\/?p=20702"},"modified":"2021-11-22T18:36:16","modified_gmt":"2021-11-22T13:06:16","slug":"python-program-to-find-square-root-under-modulo-k-when-k-is-in-form-of-4i-3","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-square-root-under-modulo-k-when-k-is-in-form-of-4i-3\/","title":{"rendered":"Python Program to Find Square Root Under Modulo k (When k is in Form of 4*i + 3)"},"content":{"rendered":"
In the previous article, we have discussed Python Program for Array\/List Elements that Appear More than Once<\/a><\/p>\n Given the number N, prime number k and the task is to find the square root of the given number under modulo k(When k is in form of 4*i + 3). Here i<\/strong> is an integer.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to find the square root of the given number under modulo k (When k is in form of 4*i + 3) in python:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Find a comprehensive collection of Examples of Python Programs<\/a> ranging from simple ones to complex ones to guide you throughout your coding journey.<\/p>\n In the previous article, we have discussed Python Program for Array\/List Elements that Appear More than Once Given the number N, prime number k and the task is to find the square root of the given number under modulo k(When k is in form of 4*i + 3). Here i is an integer. Examples: Example1: …<\/p>\nGiven prime number k=5\r\nGiven Number = 4<\/pre>\n
The Square root of the given number{ 4 } under modulo k= 2<\/pre>\n
Given prime number k=3\r\nGiven Number = 5<\/pre>\n
No, we cannot find the square root for a given number<\/pre>\n
Program to Find Square Root Under Modulo k (When k is in Form of 4*i + 3) in Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Create a function to say sqrt_undermodk which takes the given number and k as\r\n# the arguments and returns the square root of the given number under modulo k\r\n# (When k is in form of 4*i + 3).\r\n\r\n\r\ndef sqrt_undermodk(gvn_numb, k):\r\n # Calculate the value of the given number modulus k and store it in the same variable\r\n # given number.\r\n gvn_numb = gvn_numb % k\r\n # Loop from 2 to the given k value using the for loop.\r\n for itror in range(2, k):\r\n # Multiply the iterator value with itself and store it in another variable.\r\n mul = (itror * itror)\r\n # Check if the above result modulus k is equal to the given number using the if\r\n # conditional statement.\r\n if (mul % k == gvn_numb):\r\n # If it is true then print the iterator value.\r\n print(\r\n \"The Square root of the given number{\", gvn_numb, \"} under modulo k=\", itror)\r\n # Return and exit the for loop.\r\n return\r\n # Print \"No, we cannot find the square root for a given number\".\r\n print(\"No, we cannot find the square root for a given number\")\r\n\r\n\r\n# Give the prime number k as static input and store it in a variable.\r\nk = 5\r\n# Give the number as static input and store it in another variable.\r\ngvn_numb = 4\r\n# Pass the given number and k as the arguments to the sqrt_undermodk function.\r\nsqrt_undermodk(gvn_numb, k)\r\n<\/pre>\n
The Square root of the given number{ 4 } under modulo k= 2<\/pre>\n
Method #2: Using For loop (User Input)<\/h3>\n
\n
# Create a function to say sqrt_undermodk which takes the given number and k as\r\n# the arguments and returns the square root of the given number under modulo k\r\n# (When k is in form of 4*i + 3).\r\n\r\n\r\ndef sqrt_undermodk(gvn_numb, k):\r\n # Calculate the value of the given number modulus k and store it in the same variable\r\n # given number.\r\n gvn_numb = gvn_numb % k\r\n # Loop from 2 to the given k value using the for loop.\r\n for itror in range(2, k):\r\n # Multiply the iterator value with itself and store it in another variable.\r\n mul = (itror * itror)\r\n # Check if the above result modulus k is equal to the given number using the if\r\n # conditional statement.\r\n if (mul % k == gvn_numb):\r\n # If it is true then print the iterator value.\r\n print(\r\n \"The Square root of the given number{\", gvn_numb, \"} under modulo k=\", itror)\r\n # Return and exit the for loop.\r\n return\r\n # Print \"No, we cannot find the square root for a given number\".\r\n print(\"No, we cannot find the square root for a given number\")\r\n\r\n\r\n# Give the prime number k as user input using the int(input()) function and \r\n# store it in a variable.\r\nk = int(input(\"Enter some random number = \"))\r\n# Give the number as user input using the int(input()) function and \r\n# store it in another variable.\r\ngvn_numb = int(input(\"Enter some random number = \"))\r\n# Pass the given number and k as the arguments to the sqrt_undermodk function.\r\nsqrt_undermodk(gvn_numb, k)\r\n<\/pre>\n
Enter some random number = 7\r\nEnter some random number = 2\r\nThe Square root of the given number{ 2 } under modulo k= 3<\/pre>\n
\n