Question: ( e ) Implement a classical function to find the order of a modulo n where , a , n are inputs. what i have
e Implement a classical function to find the order of a modulo n where an are inputs.
what i have :
def gcdm n:
while n:
m n n m n
return m
def modularexponentiatea k n:
res
while k :
if k :
res res a n
a a a n
k
return res
def findordera n:
for r in range n:
if modularexponentiatea r n:
return r
return None
Write a function counthitsn that runs through all numbers a from to n ignoring the ones for which GCDanin which case we will find a factor purely by chance and counts the number of "hits" wherein a hit happens for a iff
what i have for this cell:
def gcdm n:
while n:
m n n m n
return m
def modularexponentiatea k n:
res
while k :
if k :
res res a n
a a a n
k
return res
def findordera n:
for r in range n:
if modularexponentiatea r n:
return r
return None
# Example usage:
printfindorder # This will return the order of mod
def counthitsn:
hits
for a in range n:
if gcda n: # a and n must be relatively prime
r findordera n
if r is not None and r : # Order must be even
if modularexponentiatea r n n :
hits
return hits
Cell that I can't change :
h counthits
printfcounthitsh
assert h # hits for are
# note that varphi
printfFraction of hits among relatively prime h
h counthits # there should be hits for
printfcounthitsh
assert h
# note that varphi
printfFraction of hits among relatively prime h
h counthits
printfcounthitsh
assert h
# note varphi
printfFraction of hits among relatively prime h
h counthits # there whould be hits
printfcounthitsh
assert h
# note varphi
printfFraction of hits among relatively prime h
h counthits
printfcounthitsh
assert h
assertion error I get:
counthits
AssertionError Traceback most recent call last Cell In line h counthits printfcounthitsh assert h # hits for are # note that varphi printfFraction of hits among relatively prime h AssertionError:
thanks for your help!
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
