Begin the euler project

This commit is contained in:
newt 2024-10-09 18:02:34 +01:00
parent a1ea242d23
commit 693642ec36
17 changed files with 4714 additions and 0 deletions

9
.prettierrc Normal file
View file

@ -0,0 +1,9 @@
{
"semi": true,
"trailingComma": "none",
"singleQuote": true,
"printWidth": 100,
"tabWidth": 4,
"bracketSpacing": true,
"arrowParens": "avoid"
}

4
euler/.gitignore vendored Normal file
View file

@ -0,0 +1,4 @@
node_modules
.yarn/cache
.yarn/install-state.gz
build

768
euler/.yarn/releases/yarn-3.1.0.cjs vendored Normal file

File diff suppressed because one or more lines are too long

2
euler/.yarnrc.yml Normal file
View file

@ -0,0 +1,2 @@
yarnPath: .yarn/releases/yarn-3.1.0.cjs
nodeLinker: node-modules

View file

@ -0,0 +1,16 @@
below = 1000
multiplesOf = [3,5]
toAdd = []
for i in range(1, below):
for num in multiplesOf:
if i % num == 0:
toAdd.append(i)
toAdd = list(dict.fromkeys(toAdd))
total = 0
for i in toAdd:
total += i
print(total)

View file

@ -0,0 +1,13 @@
sequence = [1, 2]
while sequence[len(sequence) - 1] < 4000000:
sequence.append(sequence[len(sequence) - 1] + sequence[len(sequence) - 2])
sequence.pop()
total = 0
for i in sequence:
if i % 2 == 0:
total += i
print(total)

View file

@ -0,0 +1,10 @@
n = 600851475143
i = 2
while i * i <= n:
if n % i:
i += 1
else:
n //= i
print(n)

20
euler/gulpfile.js Normal file
View file

@ -0,0 +1,20 @@
const gulp = require('gulp');
const typescript = require('gulp-typescript');
const uglify = require('gulp-uglify');
const rename = require('gulp-rename');
gulp.task('build', () => {
const tsc = typescript.createProject('tsconfig.json');
return gulp
.src('src/**/*.ts')
.pipe(tsc())
.pipe(uglify({ mangle: { toplevel: true } }))
.pipe(
rename(path => {
if (path.basename === 'utils') return;
path.basename = path.basename.substr(0, 1);
})
)
.pipe(gulp.dest('build'));
});

18
euler/package.json Normal file
View file

@ -0,0 +1,18 @@
{
"name": "project-euler",
"packageManager": "yarn@3.1.0",
"scripts": {
"build": "gulp build",
"start": "node run"
},
"devDependencies": {
"@types/node": "^16.11.6",
"chalk": "^4.1.2",
"gulp": "^4.0.2",
"gulp-cli": "^2.3.0",
"gulp-rename": "^2.0.0",
"gulp-typescript": "^6.0.0-alpha.1",
"gulp-uglify": "^3.0.2",
"typescript": "^4.4.4"
}
}

50
euler/run.js Normal file
View file

@ -0,0 +1,50 @@
const fs = require('fs');
const chalk = require('chalk');
const { spawnSync } = require('child_process');
const run = (file, puzzleName) => {
// Calculate the length of the divider for the puzzle
let divider = '--';
for (let j = 0; j < puzzleName.length; j++) {
divider += '-';
}
// Log output
console.log(divider);
console.log(chalk.bold(chalk.greenBright(puzzleName)));
console.log(divider);
spawnSync('node', [`build/${file}`], { shell: true, stdio: 'inherit' });
};
// Get files
const tsFiles = fs
.readdirSync('src')
.filter(f => f.endsWith('.ts'))
.filter(f => f !== 'utils.ts');
const jsFiles = fs
.readdirSync('build')
.filter(f => f.endsWith('.js'))
.filter(f => f !== 'utils.js');
try {
// Extract the puzzle number
const puzzleNumber = process.argv[2];
if (isNaN(puzzleNumber)) throw Error();
// Find the associated puzzle
const tsFile = tsFiles.filter(f => f.startsWith(puzzleNumber))[0];
const puzzleName = tsFile.split('.ts')[0];
run(`${puzzleNumber}.js`, puzzleName);
} catch (error) {
for (let i = 0; i < jsFiles.length; i++) {
const file = jsFiles[i];
const tsFile = tsFiles[i];
const puzzleName = tsFile.split('.ts')[0];
run(file, puzzleName);
console.log();
}
}

View file

@ -0,0 +1,12 @@
import { calcSum } from './utils';
const below = 1000;
const multiplesOf = [3, 5];
const toAdd: Set<number> = new Set();
// Find all of the multiples of 3 and 5 below 1000
for (let i = 1; i < below; i++) {
multiplesOf.forEach(num => (i % num == 0 ? toAdd.add(i) : null));
}
console.log(calcSum(Array.from(toAdd)));

View file

@ -0,0 +1,15 @@
import { calcSum } from './utils';
const sequence = [1, 2];
const max = 4000000;
// Keep making new nujm
while (sequence[sequence.length - 1] < max) {
const newValue = sequence[sequence.length - 1] + sequence[sequence.length - 2];
sequence.push(newValue);
}
// Filter out the even numbers and find the sum
const even = sequence.filter(n => n % 2 === 0);
console.log(calcSum(even));

View file

@ -0,0 +1,9 @@
let number = 600851475143;
let i = 2;
while (i * i <= number) {
if (number % i) i += 1;
else number = Math.floor(number / i);
}
console.log(number);

View file

@ -0,0 +1,22 @@
const biggest = 999;
const smallest = 100;
// Work out all of the products of 3 digit numbers
const products: number[] = [];
for (let i = smallest; i < biggest + 1; i++) {
for (let j = smallest; j < biggest + 1; j++) {
products.push(i * j);
}
}
// Filter for palindromic numbers
const palindromic = products.filter(
x => x.toString() === x.toString().split('').reverse().join('')
);
// Find the biggest palindrome number
const sorted = palindromic.sort((a, b) => b - a);
const largest = sorted[0];
console.log(largest);

4
euler/src/utils.ts Normal file
View file

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

9
euler/tsconfig.json Normal file
View file

@ -0,0 +1,9 @@
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"outDir": "build"
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules"]
}

3733
euler/yarn.lock Normal file

File diff suppressed because it is too large Load diff