Skip to content

Commit 2097017

Browse files
committed
Solved Best Divisor and Restaurant.
1 parent de6af09 commit 2097017

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ Note: Few codes were solved in C# (I've written them a long time ago), I'll conv
122122
### [Fundamentals](mathematics/fundamentals)
123123

124124
- [Army Game](mathematics/fundamentals/gameWithCells.js)
125+
- [Best Divisor](mathematics/fundamentals/bestDivisor.js)
125126
- [Connecting Towns](mathematics/fundamentals/connectingTowns.js)
126127
- [Cutting Paper Squares](mathematics/fundamentals/cuttingPapersSquare.js)
127128
- [Find the Point](mathematics/fundamentals/findPoint.js)
@@ -130,6 +131,7 @@ Note: Few codes were solved in C# (I've written them a long time ago), I'll conv
130131
- [Leonardo's Prime Factors](mathematics/fundamentals/primeCount.js)
131132
- [Maximum Draws](mathematics/fundamentals/maximumDraws.js)
132133
- [Minimum Height Triangle](mathematics/fundamentals/lowestTriangle.js)
134+
- [Restaurant](mathematics/fundamentals/restaurant.js)
133135
- [Sherlock and Moving Tiles](mathematics/fundamentals/movingTiles.js)
134136
- [Summing the N Series](mathematics/fundamentals/summingSeries.js)
135137

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//+ Jonas Raoni Soares Silva
2+
//@ http://raoni.org
3+
4+
function bestDivisor(n) {
5+
let bestSum = 0;
6+
let bestNumber = 0;
7+
for (let i = n + 1; --i; ) {
8+
// Look for a divisor
9+
if (!(n % i)) {
10+
// Lazy way to get the sum of digits (didn't want to go the Math way: "dividing by 10")
11+
const sum = [...`${i}`].reduce((t, v) => t += +v, 0);
12+
if (sum > bestSum) {
13+
bestSum = sum;
14+
bestNumber = i;
15+
}
16+
else if (sum === bestSum && i < bestNumber)
17+
bestNumber = i;
18+
}
19+
}
20+
return bestNumber;
21+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//+ Jonas Raoni Soares Silva
2+
//@ http://raoni.org
3+
4+
function restaurant(l, b) {
5+
const gcd = function(...o){
6+
let l = o.length;
7+
if(!l)
8+
return 0;
9+
let max = o[l - 1];
10+
for (let i = l - 1; i--; )
11+
for (let n = o[i], r; r = n % (n = max); max = r);
12+
return max;
13+
};
14+
// In order to divide something in equal pieces, both the width and the length should be divisible by the same number, as we need the biggest, there's the greatest common divisor
15+
const maxDivisor = gcd(l, b);
16+
return (l / maxDivisor) * (b / maxDivisor);
17+
}

0 commit comments

Comments
 (0)