freeCodeCamp/curriculum/challenges/italian/10-coding-interview-prep/project-euler/problem-99-largest-exponent...

84 lines
17 KiB
Markdown
Raw Normal View History

---
id: 5900f3d01000cf542c50fee2
title: 'Problem 99: Largest exponential'
challengeType: 5
forumTopicId: 302216
dashedName: problem-99-largest-exponential
---
# --description--
Comparing two numbers written in index form like $2^{11}$ and $3^7$ is not difficult, as any calculator would confirm that $2^{11} = 2048 < 3^7 = 2187$.
However, confirming that $632382^{518061} < 519432^{525806}$ would be much more difficult, as both numbers contain over three million digits.
Using the 2D `baseExp` array of base/exponent pairs, determine pair with the greatest numerical value and return it.
# --hints--
`largestExponential(testArray1)` should return an array.
```js
assert(Array.isArray(largestExponential(_testArray1)));
```
`largestExponential(testArray1)` should return `[840237, 507276]`.
```js
assert.deepEqual(largestExponential(_testArray1), [840237, 507276]);
```
`largestExponential(testArray2)` should return `[895447, 504922]`.
```js
assert.deepEqual(largestExponential(_testArray2), [895447, 504922]);
```
# --seed--
## --after-user-code--
```js
const _testArray1 = [
[492798,527927],[30125,670983],[895603,504906],[450785,531539],[840237,507276],[380711,538522],[63577,625673],[76801,615157],[502694,527123]
];
const _testArray2 = [
[519432,525806],[632382,518061],[78864,613712],[466580,530130],[780495,510032],[525895,525320],[15991,714883],[960290,502358],[760018,511029],[166800,575487],[210884,564478],[555151,523163],[681146,515199],[563395,522587],[738250,512126],[923525,503780],[595148,520429],[177108,572629],[750923,511482],[440902,532446],[881418,505504],[422489,534197],[979858,501616],[685893,514935],[747477,511661],[167214,575367],[234140,559696],[940238,503122],[728969,512609],[232083,560102],[900971,504694],[688801,514772],[189664,569402],[891022,505104],[445689,531996],[119570,591871],[821453,508118],[371084,539600],[911745,504251],[623655,518600],[144361,582486],[352442,541775],[420726,534367],[295298,549387],[6530,787777],[468397,529976],[672336,515696],[431861,533289],[84228,610150],[805376,508857],[444409,532117],[33833,663511],[381850,538396],[402931,536157],[92901,604930],[304825,548004],[731917,512452],[753734,511344],[51894,637373],[151578,580103],[295075,549421],[303590,548183],[333594,544123],[683952,515042],[60090,628880],[951420,502692],[28335,674991],[714940,513349],[343858,542826],[549279,523586],[804571,508887],[260653,554881],[291399,549966],[402342,536213],[408889,535550],[40328,652524],[375856,539061],[768907,510590],[165993,575715],[976327,501755],[898500,504795],[360404,540830],[478714,529095],[694144,514472],[488726,528258],[841380,507226],[328012,544839],[22389,690868],[604053,519852],[329514,544641],[772965,510390],[492798,527927],[30125,670983],[895603,504906],[450785,531539],[840237,507276],[380711,538522],[63577,625673],[76801,615157],[502694,527123],[597706,520257],[310484,547206],[944468,502959],[121283,591152],[451131,531507],[566499,522367],[425373,533918],[40240,652665],[39130,654392],[714926,513355],[469219,529903],[806929,508783],[287970,550487],[92189,605332],[103841,599094],[671839,515725],[452048,531421],[987837,501323],[935192,503321],[88585,607450],[613883,519216],[144551,582413],[647359,517155],[213902,563816],[184120,570789],[258126,555322],[502546,527130],[407655,535678],[401528,536306],[477490,529193],[841085,507237],[732831,512408],[833000,507595],[904694,504542],[581435,521348],[455545,531110],[873558,505829],[94916,603796],[720176,513068],[545034,523891],[246348,557409],[556452,523079],[832015,507634],[173663,573564],[502634,527125],[250732,556611],[569786,522139],[216919,563178],[521815,525623],[92304,605270],[164446,576167],[753413,511364],[11410,740712],[448845,531712],[925072,503725],[564888,522477],[7062,780812],[641155,517535],[738878,512100],[636204,517828],[372540,539436],[443162,532237],[571192,522042],[655350,516680],[299741,548735],[581914,521307],[965471,502156],[513441,526277],[808682,508700],[237589,559034],[543300,524025],[804712,508889],[247511,557192],[543486,524008],[504383,526992],[326529,545039],[792493,509458],[86033,609017],[126554,589005],[579379,521481],[948026,502823],[404777,535969],[265767,554022],[266876,553840],[46631,643714],[492397,527958],[856106,506581],[795757,509305],[748946,511584],[294694,549480],[409781,535463],[775887,510253],[543747,523991],[210592,564536],[517119,525990],[520253,525751],[247926,557124],[592141,520626],[346580,542492],[544969,523902],[506501,526817],[244520,557738],[144745,582349],[69274,620858],[292620,549784],[926027,503687],[736320,512225],[515528,526113],[407549,535688],[848089,506927],[24141,685711],[9224,757964],[980684,501586],[175259,573121],[489160,528216],[878970,505604],[969546,502002],[525207,525365],[690461,514675],[156510,578551],[659778,516426],[468739,529945],[765252,510770],[76703,615230],[165151,575959],[29779,671736],[928865,503569],[577538,521605],[927555,503618],[185377,570477],[974756,501809],[800130,509093],[217016,563153],[365709,540216],[774508,510320],[588716,520851],[631673,518104],[954076,502590],[777828,510161],[990659,501222],[597799,520254],[786905,509727],[512547,526348],[756449,511212],[869787,505988],[653747,516779],[84623,609900],[839698,507295],[30159,670909],[797275,509234],[678136,515373],[897144,504851],[989554,501263],[413292,535106],[55297,633667],[788650,509637],[486748,528417],[150724,580377],
];
```
## --seed-contents--
```js
function largestExponential(baseExp) {
return true;
}
// Only change code above this line
const testArray1 = [
[492798,527927],[30125,670983],[895603,504906],[450785,531539],[840237,507276],[380711,538522],[63577,625673],[76801,615157],[502694,527123]
];
largestExponential(testArray1);
```
# --solutions--
```js
function largestExponential(baseExp) {
let maximum = 0;
let indexOfMaximum = 0;
for (let i = 1; i < baseExp.length; i++) {
const curValue = Math.log(baseExp[i][0]) * baseExp[i][1];
if (curValue > maximum) {
maximum = curValue;
indexOfMaximum = i;
}
}
return baseExp[indexOfMaximum];
}
```