(euler) - #10 and export {}

This commit is contained in:
newt 2024-10-09 18:02:35 +01:00
parent 63f1701d34
commit 039be699ec
12 changed files with 57 additions and 8 deletions

View file

@ -13,7 +13,7 @@ gulp.task('build', () => {
.pipe(
rename(path => {
if (path.basename === 'utils') return;
path.basename = path.basename.substr(0, 1);
path.basename = path.basename.split('-')[0].trim();
})
)
.pipe(gulp.dest('build'));

View file

@ -1,4 +1,6 @@
import { calcSum } from './utils';
export {};
const calcSum = (numbers: number[]) => numbers.reduce((a, b) => a + b);
const multiplesOf = (numbers: number[], upperBound: number) => {
const results: Set<number> = new Set();

View file

@ -0,0 +1,35 @@
export {};
const calcSum = (numbers: number[]) => numbers.reduce((a, b) => a + b);
// Sieve of Eratosthenes time!!!
const sumOfPrimes = (upperBound: number) => {
let array: boolean[] = [];
let upperLimit = Math.sqrt(upperBound);
let output: number[] = [];
// Make an array from 2 to (n - 1) of truthy values
for (var i = 0; i < upperBound; i++) {
array.push(true);
}
// Remove multiples of primes starting from 2, 3, 5,...
for (var i = 2; i <= upperLimit; i++) {
if (array[i]) {
for (var j = i * i; j < upperBound; j += i) {
array[j] = false;
}
}
}
// All array[i] set to true are primes
for (var i = 2; i < upperBound; i++) {
if (array[i]) {
output.push(i);
}
}
return calcSum(output);
};
console.log(sumOfPrimes(2000000));

View file

@ -1,4 +1,6 @@
import { calcSum } from './utils';
export {};
const calcSum = (numbers: number[]) => numbers.reduce((a, b) => a + b);
const fibonacciNumbers = (upperBound: number) => {
const sequence = [1, 2];

View file

@ -1,3 +1,5 @@
export {};
const largestPrimeFactor = (number: number) => {
let i = 2;

View file

@ -1,3 +1,5 @@
export {};
const largestPallidromeNumber = (lowerBound: number, upperBound: number) => {
// Work out all of the products of 3 digit numbers
const products: number[] = [];

View file

@ -1,3 +1,5 @@
export {};
/**
* Is x disible to n?
*/

View file

@ -1,4 +1,6 @@
import { calcSum } from './utils';
export {};
const calcSum = (numbers: number[]) => numbers.reduce((a, b) => a + b);
const sumOfSquares = (lowerBound: number, upperBound: number) => {
// Calculate the square number of all the numbers between the bounds

View file

@ -1,3 +1,5 @@
export {};
const isPrime = (number: number) => {
for (var i = 2; i < number; i++) {
if (number % i === 0) return false;

View file

@ -1,3 +1,5 @@
export {};
const largestProduct = (number: bigint | number, adjacentDigits: number) => {
let cursorIndex = 0;
const selections: number[][] = [];

View file

@ -1,3 +1,5 @@
export {};
// a + b + c = 1000
// we need a * b * c
// pythagorean triplet: a < b < c

View file

@ -1,4 +0,0 @@
/**
* Calculates the sum of an array of numbers.
*/
export const calcSum = (numbers: number[]) => numbers.reduce((a, b) => a + b);