Time taken to get the 7th number which is 8 digits long is 8 milliseconds.
Time taken to get the 8th number which is 19 digits long is 2.6 seconds.
Still waiting for my results for the 9th number...
functionisPerfect(num){letsum=1letmax=num/2for(vari=2;i<max;i++){if(num%i===0){sum+=i// add lower divisormax=Math.floor(num/i)sum+=max//add higher divisor}}returnsum===num;}functiongetPerfectSeries(n=5){letseries=[]// Create number in binary stringletl=1while(series.length<n){lets="1"for(leti=0;i<l;i++){s+="1"}for(leti=0;i<l;i++){s+="0"}letnum=parseInt(s,2)// convert to base10if(isPerfect(num)){series.push(num)}l++}console.log(series)}functiontimer(fn){letstart=newDate();fn()lettime=newDate().getTime()-start.getTime()console.log("Time taken: "+time+"ms")}timer(()=>{getPerfectSeries(1)})// < 1mstimer(()=>{getPerfectSeries(2)})// < 1mstimer(()=>{getPerfectSeries(3)})// < 1mstimer(()=>{getPerfectSeries(4)})// < 1mstimer(()=>{getPerfectSeries(5)})// < 1mstimer(()=>{getPerfectSeries(6)})// 4mstimer(()=>{getPerfectSeries(7)})// 8mstimer(()=>{getPerfectSeries(8)})// 26.9 seconds
Anyway, this solution is inspired by @nektro
's observation of the perfect numbers - Had a suspicion that there's a geometric pattern to this which is obvious when you look like the perfect numbers is Base2.
I find base 2 and 4 particularly interesting because for each perfect number, the amount of 1s/3s and 0s is the same.
edit: fixed my sequence because 8138 is not perfect, its 8128.
Perfect numbers in binary always start with "1" followed by an equal number of "1" and "0"s.
I first generate plausible perfect numbers from binary strings first using this pattern, then converted them to Base10, before testing if it is a perfect number.
The isPerfect function can be optimised by reducing the loop to find the divisors by 1) adding both the lower and the higher divisor and 2) also setting the higher divisor as a limiter for iteration.
My solution in javascript.
Time taken to get the 7th number which is 8 digits long is 8 milliseconds.
Time taken to get the 8th number which is 19 digits long is 2.6 seconds.
Still waiting for my results for the 9th number...
Anyway, this solution is inspired by @nektro 's observation of the perfect numbers - Had a suspicion that there's a geometric pattern to this which is obvious when you look like the perfect numbers is Base2.
I find base 2 and 4 particularly interesting because for each perfect number, the amount of 1s/3s and 0s is the same.
edit: fixed my sequence because 8138 is not perfect, its 8128.
Perfect numbers in binary always start with "1" followed by an equal number of "1" and "0"s.
I first generate plausible perfect numbers from binary strings first using this pattern, then converted them to Base10, before testing if it is a perfect number.
The
isPerfect
function can be optimised by reducing the loop to find the divisors by 1) adding both the lower and the higher divisor and 2) also setting the higher divisor as a limiter for iteration.Still waiting.
@picocreator - Wanna use GPU.js to speed this up?
First dumb "look at every integer" solution:
This algo has trouble going further than 1e6.
Then, I dig your hypothesis that all Perfect number are in the form
(n+1)"1"..(n)"0"
in base 2. I needed to find the logical sequence of valid n.So I looked up this sequence on EOIS and luckily it found a match: "Numbers n such that 2n+1 - 1 is prime".
Which lead to this way more powerful script:
This time, it can't go any higher than 1e3 before crashing, but yield more results. (However, JS precision let me down on the last result)