diff --git a/maths/goldbach_conjecture.ts b/maths/goldbach_conjecture.ts new file mode 100644 index 00000000..b4b8f14b --- /dev/null +++ b/maths/goldbach_conjecture.ts @@ -0,0 +1,55 @@ +import { + takeLeft, +} from 'fp-ts/lib/Array' + +import { isPrime } from './primes' + +function range(start: number, end: number, step: number = 1): number[] { + const result: number[] = []; + for (let i = start; i < end; i += step) { + result.push(i); + } + return result; +} + +/** + * @function goldbachNumbers + * @description Return the goldbach numbers for a given even number + * @param {Number} n - a positive even number + * @return {Number} - an array of two numbers representing the goldbach numbers for N + * @see https://en.wikipedia.org/wiki/Goldbach%27s_conjecture + * @example goldbachNumbers(28) = [5, 23] + * @example goldbachNumbers(16) = [5, 11] + * @example goldbachNumbers(20) = [3, 17] + */ +export const goldbachNumbers = (n: number): number[] => { + const primes = range(2,n).filter( (m: number): Boolean => isPrime(m) && isPrime(n - m)) + if (primes.length == 0) { + [] + } + let m = takeLeft(1)(primes)[0] + return [m, n-m] +} + + +/** + * @function goldbachCompositions + * @description Return the goldbach numbers for all even numbers in a given range of positive integers, low and high + * @param {Number, Number} low and high, low < high + * @return {Number} - an array of tuples with the first element representing the even number, and the second is the two primes that sum to it. + * @see https://en.wikipedia.org/wiki/Goldbach%27s_conjecture + * @example goldbachCompositiions(20, 30) = [ + * [20,[3,17]], + * [22,[3,19]], + * [24,[5,19]], + * [26,[3,23]], + * [28,[3,25]] + * ] + */ +export const goldbachCompositions = (low: number, high: number): [number, number[]][] => { + let lo = (low % 2 == 0) ? low : low + 1 + let hi = (high % 2 == 0) ? high : high - 1 + + + return range(lo, hi, 2).map((n: number) => [n, goldbachNumbers(n)] ) +} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index ad263744..c047bbab 100644 --- a/package-lock.json +++ b/package-lock.json @@ -8,6 +8,9 @@ "name": "typescript", "version": "1.0.0", "license": "MIT", + "dependencies": { + "fp-ts": "^2.16.9" + }, "devDependencies": { "@types/jest": "^29.0.3", "husky": "^8.0.1", @@ -1757,6 +1760,12 @@ "node": ">=8" } }, + "node_modules/fp-ts": { + "version": "2.16.9", + "resolved": "https://registry.npmjs.org/fp-ts/-/fp-ts-2.16.9.tgz", + "integrity": "sha512-+I2+FnVB+tVaxcYyQkHUq7ZdKScaBlX53A41mxQtpIccsfyv8PzdzP7fzp2AY832T4aoK6UZ5WRX/ebGd8uZuQ==", + "license": "MIT" + }, "node_modules/fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", @@ -5075,6 +5084,11 @@ "path-exists": "^4.0.0" } }, + "fp-ts": { + "version": "2.16.9", + "resolved": "https://registry.npmjs.org/fp-ts/-/fp-ts-2.16.9.tgz", + "integrity": "sha512-+I2+FnVB+tVaxcYyQkHUq7ZdKScaBlX53A41mxQtpIccsfyv8PzdzP7fzp2AY832T4aoK6UZ5WRX/ebGd8uZuQ==" + }, "fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", diff --git a/package.json b/package.json index b45ab4ea..669e5349 100644 --- a/package.json +++ b/package.json @@ -19,5 +19,8 @@ "prepare": "husky install" }, "author": "TheAlgorithms", - "license": "MIT" + "license": "MIT", + "dependencies": { + "fp-ts": "^2.16.9" + } }
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: