-
Notifications
You must be signed in to change notification settings - Fork 0
/
primeAnalysis.R
95 lines (71 loc) · 1.83 KB
/
primeAnalysis.R
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
##################################
# Analysis of Primes Under N #
# M. R. Schwob #
##################################
###
### Load Output from pp.cpp
###
# load dataset
output <- read.csv("~/Desktop/Palindromes/output.txt") # change directories responsibly
# set N
N = 500000
# obtain unique values of primes featured in output
primeOutput = unique(output$Prime1)
###
### Obtain List of Primes Under N
###
prime_numbers <- function(n) {
if (n >= 2) {
x = seq(2, n)
prime_nums = c()
for (i in seq(2, n)) {
if (any(x == i)) {
prime_nums = c(prime_nums, i)
x = c(x[(x %% i) != 0], i)
}
}
return(prime_nums)
}
else
{
stop("Input number should be at least 2.")
}
}
primes = prime_numbers(N)
###
### Obtain List of Prime Numbers that Have a Mirror in Some Base (2,...,10)
###
counts = rep(FALSE, length(primes))
for(i in 1:length(primes)){
for(j in 1:length(primeOutput)){
if(primes[i] == primeOutput[j]){
counts[i] = TRUE
}
}
}
percentage = sum(counts)/length(counts)
percentage
###
### Let's Obtain a Nice Graphic
###
counter = 0
sequence = seq(1000,N,1000) # relatively decent resolution
percVec = c(rep(0,length(sequence)))
for(maxSize in sequence){
primeList = primes[primes<=maxSize]
primeOut = primeOutput[primeOutput<=maxSize]
counts = rep(FALSE, length(primeList))
for(i in 1:length(primeList)){
for(j in 1:length(primeOut)){
if(primeList[i] == primeOut[j]){
counts[i] = TRUE
}
}
}
counter = counter + 1
percVec[i] = sum(counts)/length(counts)
cat(" ", (max(sequence)-maxSize)/1000)
}
percentages = percVec[percVec>0]
options(scipen=999)
plot(sequence, percentages, type="l", xlab="Up to N", ylab="Primes with Mirrors (%)", main="Primes with Mirrors Across Different Intervals")