Skip to content

Instantly share code, notes, and snippets.

@dy
Last active September 5, 2021 00:26
Show Gist options
  • Save dy/2253b859db4e2ab4993ffe6779c1a1b5 to your computer and use it in GitHub Desktop.
Save dy/2253b859db4e2ab4993ffe6779c1a1b5 to your computer and use it in GitHub Desktop.
Do prime sums and products ever intersect?
(() => {
// play around with initial sequences / operator combinations
let seqs = {}, ops = {'+':(a,b)=>a+b,'-':(a,b)=>a-b}//'/':(a,b)=>a/b,'*':(a,b)=>a*b}
const results = new Proxy({}, {
set(results, prop, value){
if (results[prop]) {
if (!results[prop].includes(value)) console.error('set', prop, ':', value, 'exists as', results[prop])
results[prop].push(value)
return true
}
results[prop] = [value]
return true
}
})
seqs[IDS[0]] = IDS[0]
for (let i = 1; i < IDS.length; i++) {
let nseqs = {}, id = IDS[i], seqids = IDS.slice(0,i), exp
for (let seq in seqs) {
for (let op in ops) {
exp = seq + op + id
nseqs[exp] = ops[op](seqs[seq],id)
results[toHash(nseqs[exp])] = exp
// results[toHash(nseqs[exp])|0] = exp
}
// a*n + b*n + ....
// exp = `(${seq})*.5 + ${id}*.5`
// results[eval(exp)] = exp
}
// add mult
// const mexp = seqids.join('*'), mult = eval(mexp)
// results[mult] = mexp
seqs = nseqs
}
// a*n + b*n + ....
for (let m of KNOWN_COEFS) {
for (let n of KNOWN_COEFS) {
let exp = `${IDS[0]}${n===1?``:`*${n}`}+${IDS[1]}${m===1?``:`*${m}`}`
results[toHash(eval(exp))] = exp
// console.log(exp, eval(exp), toHash(eval(exp)))
// results[toHash(eval(exp))|0] = exp
}
}
})()
let n = [ 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251
].slice(1)
var res = new Map
for (let i = 0; i < n.length; i++) {
for (let j = 0; j < n.length; j++) {
let a = Math.min(n[i],n[j]), b = Math.max(n[j],n[i]), sum = n[i] + n[j], mul = n[i] * n[j]
!res.has(sum) ? res.set(sum, new Set([`${a}+${b}`])) : res.get(sum).add(`${a}+${b}`)
!res.has(mul) ? res.set(mul, new Set([`${a}*${b}`])) : res.get(mul).add(`${a}*${b}`)
// for (let k = 0; k < n.length; k++) {
// sum3.set(n[i]+n[j], [n[i], n[j]])
// mul3.set(n[i]*n[j], [n[i], n[j]])
}
}
// generate primes sequence that has guaranteed unique sum
let primes = [ 1,2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251
]
let nums = new Set(), sums = new Map(), skip = new Set()
for (let i = 0; i < primes.length; i++) {
let a = primes[i]
if (skip.has(a)) {console.log(`skip a ${a}`); continue;}
for (let j = 0; primes[j] <= a; j++) {
let b = primes[j], s = a+b
if (skip.has(b)) {console.log(`skip b ${a}+${b}`); continue;}
if (!sums.has(s)) {
console.groupCollapsed('ok', `${a}+${b}=${s}`)
sums.set(s, [a,b])
nums.add(a, b)
// cross out diagonal of all possible combinations with nums leading to that sum
// for (let k = 1; k < s; k++) {
for (let k of primes) {
for (let num of nums) {
if ((num===a && k===b) || (num===b && k===a)) continue
if (k + num === s) {skip.add(k), console.log(`✖ ${k} (${k}+${num}=${s})`)}
}
}
console.groupEnd()
} else {
throw Error(`shouldn't be there _${a}_ +${b}=${s}`)
}
}
}
// check
let narr = [...nums], checksums = new Map
for (let i = 0; i < narr.length; i++) {
for (let j = 0; j < narr.length; j++) {
let a = Math.min(narr[i],narr[j]), b = Math.max(narr[j],narr[i])
if (a === b) continue
if (checksums.has(a-b)) {checksums.get(a-b).add(`${a}-${b}`)}
else checksums.set(a-b, new Set([`${a}-${b}`]))
}
}
for (let [sum, ops] of checksums) {
if (ops.size > 1) console.log('err', ops)
}
// same as for pairs, but makes sure triple sums are unique
let primes = [ 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251
]
let nums = new Set(), sums = new Map(), skip = new Set()
for (let i = 0; i < primes.length; i++) {
let a = primes[i]
if (skip.has(a)) {console.log(`skip a ${a}`); continue;}
for (let j = 0; primes[j] <= a; j++) {
let b = primes[j]
if (skip.has(b)) {console.log(`skip b ${a}+${b}`); continue;}
for (let k = 0; primes[k] <= a; k++) {
let c = primes[k]
if (skip.has(c)) {console.log(`skip c ${a}+${b}+${c}`); continue;}
let s = a+b+c, abc = [a,b,c].sort()+''
if (!sums.has(s)) {
console.group('ok', `${a}+${b}+${c}=${s}`)
sums.set(s, abc)
nums.add(a, b, c)
// cross out plane of all possible combinations with nums leading to that sum
for (let p of primes) {
if (skip.has(p)) continue;
for (let n1 of nums) {
for (let n2 of nums) {
if (([p,n1,n2].sort()+'') === abc) continue
if (p + n1 + n2 === s) {
skip.add(p), console.log(`✖ ${p} (${p}+${n1}+${n2}=${s})`)
}
}
}
}
console.groupEnd()
} else if (sums.get(s) !== abc) {
throw Error(`shouldn't be there ${a}+${b}+${c}=${s}`)
}
}
}
}
// check
let narr = [...nums], checksums = new Map
for (let i = 0; i < narr.length; i++) {
for (let j = 0; j < narr.length; j++) {
let a = Math.min(narr[i],narr[j]), b = Math.max(narr[j],narr[i])
if (checksums.has(a+b)) {checksums.get(a+b).add(`${a}+${b}`)}
else checksums.set(a+b, new Set([`${a}+${b}`]))
}
}
for (let [sum, ops] of checksums) {
if (ops.size > 1) console.log('err', ops)
}

↑ Produces 1, 2, 5, 7, 17, 31, 53, 71, 103, 131, 193, 251

Related sequences:

Some research from older unisons

/* Machinery to enable operators overloading */
// we handpick primes so that their sums uniquely identify expression (operators sequence)
// this way we can trace back which operands are
// Research reference: https://gist.github.com/dy/2253b859db4e2ab4993ffe6779c1a1b5
// https://oeis.org/A079848
// const IDS = [/*3, 5, 11, 23, 37,*/ /*47,*/ 97, 101, /*149,*/ 211, 233, 353, 383, 487, 641, /*757,*/ /*797,*/ 919, 1097, 1163, /*1381, 1409, 1481,*/ /*1777,*/ /*1997,*/ 2287, /*2417,*/ 2969, /*3049,*/ 3371, /*3529,*/ 3929, 4231, 4759, 5279, 5449, /*5717,*/ /*5953,*/ /*6529,*/ /*6983, 7583,*/ /*8053,*/ 8819, 9043]
// has many non-sum ops intersections
// can't start with 2 because that's the only case where sum/products intersect
// sum/dif matches:
// 3 + 5 = 11 - 3
// 37 - 3 = 11 + 23
// 5 + 37 = 47 - 5
// ...
// rational matches:
// (5717 + 3049) * .11(1) = 487 * 2
// (3929 + 3049) * .33(3) = 1163 * 2
// (5717 - 3929) * .16(6) = 149 * 2
// (2287 + 97) * .125 = 149 * 2
// (6529 - 4231) * .33(3) = 383 * 2
// (6529 + 1163) * .125 = 641 * 1.5
// 5449 - 211 = (97 * 99) * 6 / 11
// (2969 - 641) * .125 = (97 * 3)
// 3929 - 919 = (101 * 8) / .16
// (61 + 101) = (211 - 61) * 1.08
// ...
// https://oeis.org/A001333 - mysteriously generates unique results for seqs like ±a ± b ± c ± ...
// export const IDS = [ 1, 3, 7, 17, 41, 99, 239, 577, 1393, 3363 ]
// https://oeis.org/A000244 - even funnier, powers of 3 generate unique results for seqs ±a ± b ± c..., ±b ± c ± d...,
// a*b*c... moves order up, so safely detected
export const IDS = [ 1,3,9,27,81,243,729,2187,6561 ]
IDS.current = 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment