0

I want to solve this equation x^2= 4 mod 3 in R, I tried many commands but that commands are not related to this equation. Please help me to find the command. For example, I tried

modlin(2,4,3)
# [1] 2

modpower(2,4,3)
# [1] 1

and five other commands.

sebastian-c
  • 15,057
  • 3
  • 47
  • 93

1 Answers1

0

Here is a self-made function that solves x^2=n mod m:

f <- function(n,m){which(((0:(m-1))^2 %% m)==(n %% m))[1] - 1}

.

> f(4,3)
[1] 1
> sapply(-10:10,f,5)
 [1]  0  1 NA NA  2  0  1 NA NA  2  0  1 NA NA  2  0  1 NA NA  2  0
> 
mra68
  • 2,960
  • 1
  • 10
  • 17