You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/* Part 2: Finding the PSR (Psuedo Square Root) */
26
+
/*
27
+
The pseudo square root of n is defined as the LARGEST divisor of n that doesn't exceed sqrt(n)
28
+
The pseudo square root of the product of all the primes below 190 is thus going to be a number composed of primes below 190 (with each prime occuring exactly once) such that it doesnot exceed sqrt(n)
29
+
30
+
To make calculations easier, we can log everything, remembering our log properties:
31
+
log(ab) = log(a) + log(b)
32
+
and
33
+
log(a^b) = b*log(a)
34
+
*/
35
+
36
+
constlogOfPrimeNumbersBelow190=[];
37
+
38
+
primeNumbersBelow190.forEach(p=>{
39
+
logOfPrimeNumbersBelow190.push(Math.log(p));
40
+
});
41
+
42
+
//our "target" is going to be the log of the square root of the product of all the prime numbers below 190, which we can find by summing the array and dividing by 2, based on log properties
//helper function to return an array of objects containing attributes subset and sum, containing all possible subsets of an inputted array
54
+
functionsubsetSums(arr){
55
+
letsubsets=[[]];
56
+
letresult=[];
57
+
58
+
//iterate through items in the array
59
+
for(leti=0;i<arr.length;i++){
60
+
letcurrentSubsets=subsets.slice();//create a temporary copy of subsets
61
+
62
+
for(letj=0;j<currentSubsets.length;j++){
63
+
letnewSubset=currentSubsets[j].concat(arr[i]);//add the current element to each existing subset to make new subsets and then add the new subsets to the array of subsets
64
+
subsets.push(newSubset);
65
+
}
66
+
}
67
+
68
+
//sum all the subsets
69
+
subsets.forEach((subset)=>{
70
+
letsum=subset.reduce((accumulator,element)=>{
71
+
return(accumulator+element)
72
+
},0);
73
+
74
+
result.push({
75
+
subset: subset,
76
+
sum: sum
77
+
});
78
+
});
79
+
80
+
returnresult;
81
+
}
82
+
83
+
constfirstHalfSums=subsetSums(firstHalf);
84
+
constsecondHalfSums=subsetSums(secondHalf);
85
+
86
+
//sort them in ascending order
87
+
firstHalfSums.sort((a,b)=>a.sum-b.sum);
88
+
secondHalfSums.sort((a,b)=>a.sum-b.sum);
89
+
90
+
letfirstHalfIterator=0;
91
+
letsecondHalfIterator=secondHalfSums.length-1;
92
+
93
+
letclosestSumFoundSoFar={
94
+
sum: -Infinity,
95
+
firstHalfIterator: null,
96
+
secondHalfIterator: null
97
+
};
98
+
99
+
//iterate through the 2 arrays, starting at the beginning of the first one and at the end of the first one
0 commit comments