Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
22
Task/Stern-Brocot-sequence/R/stern-brocot-sequence-1.r
Normal file
22
Task/Stern-Brocot-sequence/R/stern-brocot-sequence-1.r
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
## Stern-Brocot sequence
|
||||
## 12/19/16 aev
|
||||
SternBrocot <- function(n){
|
||||
V <- 1; k <- n/2;
|
||||
for (i in 1:k)
|
||||
{ V[2*i] = V[i]; V[2*i+1] = V[i] + V[i+1];}
|
||||
return(V);
|
||||
}
|
||||
|
||||
## Required tests:
|
||||
require(pracma);
|
||||
{
|
||||
cat(" *** The first 15:",SternBrocot(15),"\n");
|
||||
cat(" *** The first i@n:","\n");
|
||||
V=SternBrocot(40);
|
||||
for (i in 1:10) {j=match(i,V); cat(i,"@",j,",")}
|
||||
V=SternBrocot(1200);
|
||||
i=100; j=match(i,V); cat(i,"@",j,"\n");
|
||||
V=SternBrocot(1000); j=1;
|
||||
for (i in 2:1000) {j=j*gcd(V[i-1],V[i])}
|
||||
if(j==1) {cat(" *** All GCDs=1!\n")} else {cat(" *** All GCDs!=1??\n")}
|
||||
}
|
||||
21
Task/Stern-Brocot-sequence/R/stern-brocot-sequence-2.r
Normal file
21
Task/Stern-Brocot-sequence/R/stern-brocot-sequence-2.r
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
genNStern <- function(n)
|
||||
{
|
||||
sternNums <- c(1, 1)
|
||||
i <- 2
|
||||
while((endIndex <- length(sternNums)) < n)
|
||||
{
|
||||
#To show off R's vectorization, the following line is deliberately terse.
|
||||
#It assigns sternNums[i-1]+sternNums[i] to sternNums[endIndex+1]
|
||||
#and it assigns sternNums[i], the "considered" number, to sternNums[endIndex+2], now the end of the sequence.
|
||||
#Note that we do not have to initialize a big sternNums array to do this.
|
||||
#True to the algorithm, the new entries are appended to the end of the old sequence.
|
||||
sternNums[endIndex + c(1, 2)] <- c(sum(sternNums[c(i - 1, i)]), sternNums[i])
|
||||
i <- i + 1
|
||||
}
|
||||
sternNums[seq_len(n)]
|
||||
}
|
||||
#N=5000 was picked arbitrarily. The code runs very fast regardless of this number being much more than we need.
|
||||
firstFiveThousandTerms <- genNStern(5000)
|
||||
match(1:10, firstFiveThousandTerms)
|
||||
match(100, firstFiveThousandTerms)
|
||||
all(sapply(1:999, function(i) gmp::gcd(firstFiveThousandTerms[i], firstFiveThousandTerms[i + 1])) == 1)
|
||||
Loading…
Add table
Add a link
Reference in a new issue