So I have written a program (see below) to run a series of test cases and for each case determine if X exists for A≡X^2(modM) using Euler's Criterion. It works fine for small test cases, such as when A = 0 and M = 2 or A = 4 and M = 7, but when I consider larger test cases, such as when A = 83715323 and M = 118299443, the program just hangs on line 15 (shown below). Why is this program hanging? Is it a limit of python? Should I perhaps just redo it in something like C++?
The Problem Line
if((A[case]**((M[case] - 1)/2) - 1) % M[case]) == 0:
The Whole Program
number_of_cases = int(raw_input())
A = []
M = []
for case in range(number_of_cases):
A_M = raw_input().split()
A.append(int(A_M[0]))
M.append(int(A_M[1]))
for case in range(number_of_cases):
solution_exists = 0
if((A[case]**((M[case] - 1)/2) - 1) % M[case]) == 0:
print ("YES")
break
else:
print("NO")