Skip to content

Commit badbe39

Browse files
committed
Solved some Mathematics/Fundamentals tests.
1 parent 9ddaecc commit badbe39

File tree

9 files changed

+82
-3
lines changed

9 files changed

+82
-3
lines changed

README.md

+14-3
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,24 @@ Note: Few codes were solved in C# (I've written them a long time ago), I'll conv
9292
## [Functional Programming](functional-programming)
9393

9494
- [Solve Me First FP](functional-programming/introduction/solveMeFirst.cs)
95+
9596
### [Introduction](functional-programming/introduction)
9697

98+
## [Mathematics](mathematics)
99+
100+
### [Fundamentals](mathematics/fundamentals)
101+
102+
- [Army Game](mathematics/fundamentals/gameWithCells.js)
103+
- [Find the Point](mathematics/fundamentals/findPoint.js)
104+
- [Handshake](mathematics/fundamentals/handshake.js)
105+
- [Is Fibo](mathematics/fundamentals/solve.js)
106+
- [Leonardo's Prime Factors](mathematics/fundamentals/primeCount.js)
107+
- [Maximum Draws](mathematics/fundamentals/maximumDraws.js)
108+
- [Minimum Height Triangle](mathematics/fundamentals/lowestTriangle.js)
109+
97110
## [Tutorials](tutorials)
98111

99112
### [Cracking The Code Interview](tutorials/cracking-the-code-interview)
100-
Merge Sort: Counting Inversions
101113
- [Arrays: Left Rotation](tutorials/cracking-the-code-interview/rotLeft.js)
102114
- [Hash Tables: Ice Cream Parlor](tutorials/cracking-the-code-interview/whatFlavors.js)
103115
- [Hash Tables: Ransom Note Solution](tutorials/cracking-the-code-interview/checkMagazine.js)
@@ -107,5 +119,4 @@ Merge Sort: Counting Inversions
107119
- [Sorting: Bubble Sort](tutorials/cracking-the-code-interview/countSwaps.js)
108120
- [Sorting: Comparator](tutorials/cracking-the-code-interview/compare.js)
109121
- [Strings: Making Anagrams](tutorials/cracking-the-code-interview/makeAnagram.js)
110-
- [Time Complexity: Primality](tutorials/cracking-the-code-interview/primality.js)
111-
122+
- [Time Complexity: Primality](tutorials/cracking-the-code-interview/primality.js)

mathematics/fundamentals/findPoint.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//+ Jonas Raoni Soares Silva
2+
//@ http://raoni.org
3+
4+
function findPoint(px, py, qx, qy) {
5+
return [qx * 2 - px, qy * 2 - py];
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//+ Jonas Raoni Soares Silva
2+
//@ http://raoni.org
3+
4+
function gameWithCells(n, m){
5+
const squares = (n >> 1) * (m >> 1);
6+
const conectors = Math.ceil((n * m - squares * 4) / 2);
7+
return squares + conectors;
8+
}

mathematics/fundamentals/handshake.js

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//+ Jonas Raoni Soares Silva
2+
//@ http://raoni.org
3+
4+
function handshake(n) {
5+
return n * (n - 1) / 2;
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//+ Jonas Raoni Soares Silva
2+
//@ http://raoni.org
3+
4+
function lowestTriangle(base, area){
5+
return Math.ceil(2 * area / base);
6+
}
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//+ Jonas Raoni Soares Silva
2+
//@ http://raoni.org
3+
4+
function maximumDraws(n) {
5+
return n + 1;
6+
}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//+ Jonas Raoni Soares Silva
2+
//@ http://raoni.org
3+
4+
// My solution will fail without updating the "main()", I've replaced the `parseInt(readLine(), 10)` by `BigInt(readLine())` =]
5+
function primeCount(n) {
6+
if (n <= 1)
7+
return 0;
8+
let count = 1;
9+
// Generate and multiply each prime, to know what's the max factor that can be built with them within the given interval
10+
next:
11+
for (let current = 2n, i = 1; (i += 2) <= n; ) {
12+
// Reducing the number of checks
13+
const end = Math.sqrt(i) >> 0;
14+
for (let j = 1; ++j <= end; )
15+
if (!(i % j))
16+
continue next;
17+
if ((current *= BigInt(i)) > n)
18+
break;
19+
++count;
20+
}
21+
return count;
22+
}

mathematics/fundamentals/solve.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
//+ Jonas Raoni Soares Silva
2+
//@ http://raoni.org
3+
4+
function solve(n) {
5+
// I would add some caching here, to continue from the biggest found, but it wasn't needed :D
6+
let last = 1;
7+
for (let penultimate = 0, i = 0; last < n;)
8+
last = penultimate + (penultimate = last);
9+
return last === n ? 'IsFibo' : 'IsNotFibo';
10+
}

tutorials/cracking-the-code-interview/fibonacci.js

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
//@ http://raoni.org
33

44
function fibonacci(n) {
5+
return Math.round(Math.pow((Math.sqrt(5) + 1) / 2, Math.abs(n)) / Math.sqrt(5)) * (n < 0 && n % 2 ? -1 : 1);
6+
}
7+
8+
function fibonacci2(n) {
59
if (n < 2)
610
return n;
711
let last = 1;

0 commit comments

Comments
 (0)