Last active
May 1, 2020 23:56
-
-
Save dlebauer/cfa695f73035c1dd46b49be15c6a1673 to your computer and use it in GitHub Desktop.
Exercise from math circle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://stats.stackexchange.com/a/1214/1381 | |
find.freq <- function(x) | |
{ | |
n <- length(x) | |
spec <- spec.ar(c(x),plot=FALSE) | |
if(max(spec$spec)>10) # Arbitrary threshold chosen by trial and error. | |
{ | |
period <- round(1/spec$freq[which.max(spec$spec)]) | |
if(period==Inf) # Find next local maximum | |
{ | |
j <- which(diff(spec$spec)>0) | |
if(length(j)>0) | |
{ | |
nextmax <- j[1] + which.max(spec$spec[j[1]:500]) | |
period <- round(1/spec$freq[nextmax]) | |
} | |
else | |
period <- 1 | |
} | |
} | |
else | |
period <- 1 | |
return(period) | |
} | |
n <- 0:10 | |
y <- x%%7 | |
x <- 2^n | |
x%%13 | |
n <- 0:200 | |
x <- 2^n | |
# primes <- randtoolbox::get.primes(n = 10) | |
primes <- c(2, 3, 5, 7, 11, 13, 17, 19, 23, 29) | |
period <- vector(length = length(primes)) | |
for (i in 1:length(primes)){ | |
prime <- primes[i] | |
period[i] <- find.freq(x%%prime) | |
} | |
plot(primes, period) |
Author
dlebauer
commented
May 1, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment