-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp52.R
More file actions
41 lines (36 loc) · 1002 Bytes
/
Copy pathp52.R
File metadata and controls
41 lines (36 loc) · 1002 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# [1] "x = 142857"
# user system elapsed
# 6.753 0.000 6.741
main <- function(){
pmt <- proc.time()
x <- 100
repeat{
x <- x+1
if(!hasLeading1(x)){
x <- 10^(length(strsplit(as.character(x),"")[[1]]))
next
}
if(hasSameDigits(x,6*x)){
if(hasSameDigits(x,5*x)){
if(hasSameDigits(x,4*x)){
if(hasSameDigits(x,3*x)){
if(hasSameDigits(x,2*x)){
print(paste("x =",x))
break
}
}
}
}
}
#if(x%%100==0){print(x)}
}
print(proc.time()-pmt)
}
hasSameDigits <- function(a,b){
a <- sort(as.integer(strsplit(as.character(a),"")[[1]]))
b <- sort(as.integer(strsplit(as.character(b),"")[[1]]))
identical(a,b)
}
hasLeading1 <- function(a){
as.numeric(strsplit(as.character(a),"")[[1]])[1]==1
}