-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp36.R
More file actions
34 lines (30 loc) · 886 Bytes
/
Copy pathp36.R
File metadata and controls
34 lines (30 loc) · 886 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
#872187
# 18.17 seconds
main <- function(){
pmt <- proc.time()
a <- seq(1,1000000)
b <- sapply(a,isPal)
c <- a[b] #base10 pals
d <- sapply(c,myIntToBit)
e <- sapply(d,isPal)
print(sum(c[e]))
print(proc.time()-pmt)
}
#can't handle large numbers
num2bit <- function(num){
as.integer(paste(rev(as.integer(intToBits(num))), collapse=""))
}
#my home grown function for large numbers
myIntToBit <- function(num){
vec <- vector(mode = "integer")
repeat{
vec <- c(vec, num%%2)
num <- floor(num/2)
if(num<2){break}
}
(paste(rev(c(vec,1)), collapse = ""))#used to return an int, but had rounding errors
}
isPal <- function(gate){
gate <- as.character(gate)
identical(gate, paste(rev(strsplit(gate, "")[[1]]), collapse=""))
}