Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions Project-Euler/Problem053.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
// https://projecteuler.net/problem=53
There are exactly ten ways of selecting three from five, 12345:
123, 124, 125, 134, 135, 145, 234, 235, 245, 345
In combinatorics, we use the notation, 5C3 = 10
In general, nCr = n!/(r!(n-r)!), where r <= n
n! = n*(n-1)*...*3*2*1, and 0! = 1
It is not until n=23, that a value exceeds one-million: 23C10 = 1144066
How many, not necessarily distinct, value of nCr for 1 <= n <= 100 are greater than one-million?
*/
export const answer = (minVal, maxVal) => {
/*
When n is fixed, the maximum value of nCr is when r is the closes to n/2.
//Since nCr = nC(n-r), loop from the ceil of n/2 and increase by one until
the value becomes less than or equal to one million. Then, multiply by two
because of symmetry, but if n is even, then subtract one to avoid overcounting.
*/
var factorials = [1];
var maxR = 0;
var numWork = 0;
var totalNumWork = 0;
for (var i=1;i<=maxVal;i++){
factorials.push(factorials[factorials.length-1]*i);
}
for (var n=minVal;n<=maxVal;n++){
maxR = Math.ceil(n/2);
numWork = 0;
for (var r=maxR;r<=n;r++){
if (factorials[n]/(factorials[r]*factorials[n-r]) > 1000000){
numWork++;
}
else{
break;
}
}
if (n%2 == 1){
numWork = 2*numWork;
}
else{
numWork = Math.max(0, 2*numWork-1);
}
totalNumWork += numWork;
}
return totalNumWork
}
10 changes: 10 additions & 0 deletions Project-Euler/test/Problem053.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { answer } from '../Problem053.js'

describe('Number of values of nCr that exceed 1 million when n is in a certain range', () => {
test('range is 10 to 100', () => {
expect(answer(10,100)).toBe(4075)
})
test('range is 1 to 150', () => {
expect(answer(1,150)).toBe(10000)
})
})