-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalgorithm-semiprime-factoring-naive.js
107 lines (82 loc) · 2.28 KB
/
algorithm-semiprime-factoring-naive.js
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
96
97
98
99
100
101
102
103
104
105
106
107
const logger = require('../src/logger')()
const math = require('mathjs')
// a naive brute force illustration of the concept behind Shor's algorithm
// this illustration finds the two prime numbers which are the prime factors of a semiprime product
// prime a * prime b = the semiprime product
// this will not scale well to very large sets of prime numbers
let primes = new Primes({ max: 1000 })
let host = new Host()
let oracle = new Oracle()
let a = host.test(oracle)
let b = oracle.sibling(a)
let semiprime = a * b
logger.log('')
logger.log(`The host found one prime factor of ${a}.`)
logger.log(`The oracle acknowledged the other prime factor of ${b}.`)
logger.log(`The factor ${a} times the factor ${b} equals the semiprime product ${semiprime}.`)
logger.log(`${a} * ${b} = ${semiprime}.`)
logger.log(`Does the oracle confirm this semiprime? ${oracle.confirm(semiprime)}`)
logger.log('')
function Host() {
Object.assign(this, {
test: function(oracle) {
let result = null
primes.iterate(function(prime) {
if (oracle.query(prime) === 0) {
result = prime
return 'break'
}
}.bind(this))
return result
}
})
}
function Oracle() {
Object.assign(this, {
initialize: function() {
let factors = []
factors.push(primes.random())
factors.push(primes.random())
this.semiprime = factors[0] * factors[1]
},
query: function(value) {
return this.semiprime % value
},
sibling: function(prime) {
return this.semiprime / prime
},
confirm: function(semiprime) {
return semiprime === this.semiprime ? 'yes' : 'no'
}
})
this.initialize()
}
function Primes(options) {
Object.assign(this, {
initialize: function() {
this.array = []
let max = options && options.max ? options.max : 100
repeat(max, function(index) {
if (math.isPrime(index)) {
this.array.push(index)
}
}.bind(this))
},
random: function() {
return this.array[Math.floor(Math.random() * this.array.length)]
},
iterate: function(fn) {
repeat(this.array.length, function(index) {
let prime = this.array[index]
return fn(prime)
}.bind(this))
}
})
this.initialize()
}
function repeat(number, fn) {
for (let i = 0; i < number; i++) {
let result = fn.apply(this, [i])
if (result === 'break') break
}
}