Back to CerebralDatabank's Blog

# Cheesing a Math Problem with Code

It's definitely been a while since I posted things here! I'll use this blog space to share anything interesting that I feel like sharing here; don't expect a regular schedule. 😜 Also, I'm going to start using Prism for code syntax highlighting and KaTeX for math typesetting.

One more thing: It's Wikipedia's 20th birthday today (2020-01-15)!! Wikipedia is truly one of the greatest things we have; it's a vast, free, and open repository of human knowledge. Go donate to them if you can!

Back to the topic at hand: I am in a mathematics-related club and was presented with the following weekly problem.

$$\displaystyle \text{Consider the function } g \text{ such that } g(x) + g(x - 1) = x^2 \text{ and } g(17) = 153 \text{. What is } g(100) \text{? Bonus points if you can name the set formed by } \{ g(1), g(2), g(3), g(4), \ldots \} \text{.}$$

I realized that I could solve it by repeatedly using the recursive function forwards or backwards until I got to my desired value. So, I created a .JS file, pulled up VS Code, started coding, and a few minutes later had my final code. (I've also included the comments I added after running the code.):

        
/* PROBLEM:
* Consider the function g such that g(x) + g(x − 1) = x² and g(17) = 153.
* What is g(100)?
* Bonus points if you can name the set formed by {g(1), g(2), g(3), g(4), …}
*
*
* WORK:
* Since g(x) = x² − g(x − 1),
* g(x + 1) = (x + 1)² − g(x).
* g(x − 1) = x² − g(x)
*/

let resArr = [];
/*Calculating values of g(x) for 0 ≤ x ≤ 17*/
let currValue = 153; /*Value of g(17)*/
for (let i = 16; i >= 0; i--) {
currValue = (i + 1) ** 2 - currValue; /*x² − g(x); i is (x − 1), currValue is value of g(x) for previous x*/
resArr.unshift(g(${i}) =${currValue}); /*Insert values into array in reverse order since it's calculated from 16 to 0*/
}
resArr.push(g(17) = 153 (given starting value));
/*Calculating values of g(x) for 18 ≤ x ≤ 100*/
currValue = 153;
for (let i = 18; i <= 100; i++) {
currValue = i ** 2 - currValue; /*(x + 1)² − g(x); i is (x + 1), currValue is value of g(x) for previous x*/
resArr.push(g(${i}) =${currValue});
}
resArr.forEach(item => {
console.log(item);
});

/* ANSWER (found from running program):
* g(100) = 5050
* {g(1), g(2), g(3), g(4), …} = {0, 1, 3, 6, …}
* The above set is the set of triangular (or triangle) numbers.
*/



Running the code above gave me the following output:

        
g(0) = 0
g(1) = 1
g(2) = 3
g(3) = 6
g(4) = 10
g(5) = 15
g(6) = 21
g(7) = 28
g(8) = 36
g(9) = 45
g(10) = 55
g(11) = 66
g(12) = 78
g(13) = 91
g(14) = 105
g(15) = 120
g(16) = 136
g(17) = 153 (given starting value)
g(18) = 171
g(19) = 190
g(20) = 210
g(21) = 231
g(22) = 253
g(23) = 276
g(24) = 300
g(25) = 325
g(26) = 351
g(27) = 378
g(28) = 406
g(29) = 435
g(30) = 465
g(31) = 496
g(32) = 528
g(33) = 561
g(34) = 595
g(35) = 630
g(36) = 666
g(37) = 703
g(38) = 741
g(39) = 780
g(40) = 820
g(41) = 861
g(42) = 903
g(43) = 946
g(44) = 990
g(45) = 1035
g(46) = 1081
g(47) = 1128
g(48) = 1176
g(49) = 1225
g(50) = 1275
g(51) = 1326
g(52) = 1378
g(53) = 1431
g(54) = 1485
g(55) = 1540
g(56) = 1596
g(57) = 1653
g(58) = 1711
g(59) = 1770
g(60) = 1830
g(61) = 1891
g(62) = 1953
g(63) = 2016
g(64) = 2080
g(65) = 2145
g(66) = 2211
g(67) = 2278
g(68) = 2346
g(69) = 2415
g(70) = 2485
g(71) = 2556
g(72) = 2628
g(73) = 2701
g(74) = 2775
g(75) = 2850
g(76) = 2926
g(77) = 3003
g(78) = 3081
g(79) = 3160
g(80) = 3240
g(81) = 3321
g(82) = 3403
g(83) = 3486
g(84) = 3570
g(85) = 3655
g(86) = 3741
g(87) = 3828
g(88) = 3916
g(89) = 4005
g(90) = 4095
g(91) = 4186
g(92) = 4278
g(93) = 4371
g(94) = 4465
g(95) = 4560
g(96) = 4656
g(97) = 4753
g(98) = 4851
g(99) = 4950
g(100) = 5050



Perfect! Now I knew that $$\displaystyle g(100) = 5050$$ and the set $$\displaystyle \{ g(1), g(2), g(3), g(4), \ldots \} = \{ 1, 3, 6, 10, \ldots \}$$ was the set of triangular numbers.

I still haven't endeavored to understand the actual method to solve it, but I'll get to that eventually. 😀