(euler) - #10 and export {}
This commit is contained in:
parent
63f1701d34
commit
039be699ec
12 changed files with 57 additions and 8 deletions
|
@ -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'));
|
||||
|
|
|
@ -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();
|
||||
|
|
35
euler/src/10 - Sumation of Primes.ts
Normal file
35
euler/src/10 - Sumation of Primes.ts
Normal 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));
|
|
@ -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];
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
export {};
|
||||
|
||||
const largestPrimeFactor = (number: number) => {
|
||||
let i = 2;
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
export {};
|
||||
|
||||
const largestPallidromeNumber = (lowerBound: number, upperBound: number) => {
|
||||
// Work out all of the products of 3 digit numbers
|
||||
const products: number[] = [];
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
export {};
|
||||
|
||||
/**
|
||||
* Is x disible to n?
|
||||
*/
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
export {};
|
||||
|
||||
const isPrime = (number: number) => {
|
||||
for (var i = 2; i < number; i++) {
|
||||
if (number % i === 0) return false;
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
export {};
|
||||
|
||||
const largestProduct = (number: bigint | number, adjacentDigits: number) => {
|
||||
let cursorIndex = 0;
|
||||
const selections: number[][] = [];
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
export {};
|
||||
|
||||
// a + b + c = 1000
|
||||
// we need a * b * c
|
||||
// pythagorean triplet: a < b < c
|
||||
|
|
|
@ -1,4 +0,0 @@
|
|||
/**
|
||||
* Calculates the sum of an array of numbers.
|
||||
*/
|
||||
export const calcSum = (numbers: number[]) => numbers.reduce((a, b) => a + b);
|
Loading…
Reference in a new issue