Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
35
Task/Farey-sequence/R/farey-sequence.r
Normal file
35
Task/Farey-sequence/R/farey-sequence.r
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
farey <- function(n, length_only = FALSE) {
|
||||
a <- 0
|
||||
b <- 1
|
||||
c <- 1
|
||||
d <- n
|
||||
if (!length_only)
|
||||
cat(a, "/", b, sep = "")
|
||||
count <- 1
|
||||
while (c <= n) {
|
||||
count <- count + 1
|
||||
k <- ((n + b) %/% d)
|
||||
next_c <- k * c - a
|
||||
next_d <- k * d - b
|
||||
a <- c
|
||||
b <- d
|
||||
c <- next_c
|
||||
d <- next_d
|
||||
if (!length_only)
|
||||
cat(" ", a, "/", b, sep = "")
|
||||
}
|
||||
if (length_only)
|
||||
cat(count, "items")
|
||||
cat("\n")
|
||||
}
|
||||
|
||||
|
||||
for (i in 1:11) {
|
||||
cat(i, ": ", sep = "")
|
||||
farey(i)
|
||||
}
|
||||
|
||||
for (i in 100 * 1:10) {
|
||||
cat(i, ": ", sep = "")
|
||||
farey(i, length_only = TRUE)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue