Problem 23: Non-abundant sums

I do not understand the reason why this is not working. I am testing until 100 manually and it is operating correctly.

let sumOfProperDivisors = 0;
let number = 0;
let arrayOfAbundantNumbers = [];
let sumOfDeficientNumbers = 0;
let sumOfAbundantNumbers = 0;
let sumOfNumbersWhichCannotBeAdded = 0;
let arrayOfNumbersWhichCannotBeAdded = [];
let limit = 100;
      
      while(number<limit){

			for(let i = 1;i<number;i++){
				
					if(number%i==0){
				    
				           sumOfProperDivisors = sumOfProperDivisors + i; 
												              	
				   		          };
					  };

			if(sumOfProperDivisors>number){
							         arrayOfAbundantNumbers.push(number);
							         sumOfAbundantNumbers=sumOfAbundantNumbers + number; 
							}else{
			                         sumOfDeficientNumbers = sumOfDeficientNumbers + number;
							};

			for(let i = 0;i<arrayOfAbundantNumbers.length;i++){
			        		for(let j = 0;j<arrayOfAbundantNumbers.length;j++){
			        				if(number==arrayOfAbundantNumbers[i] + arrayOfAbundantNumbers[j]){
			                                    arrayOfNumbersWhichCannotBeAdded.push(number);
			                                    sumOfNumbersWhichCannotBeAdded = sumOfNumbersWhichCannotBeAdded + number; 
			                                    //console.log("we cannot add this number to sumOfDeficientNumbers ");

									};	
									break;
                            };
			        };

			    number = number + 1; 
                sumOfProperDivisors = 0;

			};

			total = sumOfDeficientNumbers + sumOfAbundantNumbers  - sumOfNumbersWhichCannotBeAdded;

That nesting makes me suspicious of you timing out

1 Like

No, the nest is not causing me problems. I am getting less than 2 seconds with 28123. The problem is that I am getting a high sum of integers and I do not understand the reason why.

2 seconds is pretty long. That’s a problem.

1 Like

Let me solve that first, then I will come back with a question. Thanks!

I am getting the results with the code, but I am unsure how to optimize it. Do you have any suggestions?

while(number<limit){
		for(let i = 0;i<number;i++){
    
    	  if(number%i==0){
            
            listOfDivisors.push(i);
    	  	sumOfDivisors = sumOfDivisors + i ; 

      	};

	};

	if(sumOfDivisors>number){

        listOfAbudantNumbers.push(number);
	
	};

	sumOfDivisors = 0;
	number = number + 1;

	};			

	for(let i = 0;i<listOfAbudantNumbers .length;i++){
		for(let j = 0;j<listOfAbudantNumbers .length;j++){
                            listOfNumbersThatAreTheSumOfTwoAbundantNumbers.push(listOfAbudantNumbers[i] + listOfAbudantNumbers[j]);
		};

	};

    for(let i = 0;i<limit;i++){

         listOfNumbersUntilLimit.push(i);     
    
    };

    const difference1 = listOfNumbersUntilLimit.filter(item => !listOfNumbersThatAreTheSumOfTwoAbundantNumbers.includes(item));
	
	for(let i = 0 ;i<difference1.length;i++){

      sumOfIntegerThatAreNotTheSumOfTwoAbudantNumbers = sumOfIntegerThatAreNotTheSumOfTwoAbudantNumbers + difference1[i];   
	
	};	

    console.log(sumOfIntegerThatAreNotTheSumOfTwoAbudantNumbers);

Dynamically creating a huge array on each iteration is going to be super slow

1 Like

I made it a little faster with the abundant number feature that indicates every number after 20161 is abundant.

But it is not enough to pass the tests because it is too slow yet.

Do you have any suggestions?

let divisorsOfAnumber=0;
				let number=1;
				let abudantNumber=0;
				let upperBoundLimit=20161;
			    let arrayOfAbundantNumbers=[];
				let iteration = 1;
				let arrayOfIntegerNumbers = [];
				let sumOfAllNumbers = 0;
				let arrayOfTwoAbundantSum = [];
				
				
				
						 while(number<=upperBoundLimit){
								for(let i = 0;i<number;i++){
									if(number%i==0){
										
										divisorsOfAnumber= divisorsOfAnumber + i;
									};
				
								};
				
								if(divisorsOfAnumber>number){
									abudantNumber=number;
			                        arrayOfAbundantNumbers.push(abudantNumber);						
								//console.log("The number "+abudantNumber+" is an abundant number");
								};

								number = number + 1;
								divisorsOfAnumber = 0;

							};

							for(let i = 0;i<arrayOfAbundantNumbers.length;i++){
								for(let j = 0;j<arrayOfAbundantNumbers.length;j++){
									arrayOfTwoAbundantSum.push(arrayOfAbundantNumbers[i]+arrayOfAbundantNumbers[j]);
								};

							};

							//console.log(arrayOfTwoAbundantSum);



							for(let i = 1;i<=upperBoundLimit;i++){
								arrayOfIntegerNumbers.push(i);
							};

							//console.log(arrayOfIntegerNumbers,arrayOfAbundantNumbers);

							let res = arrayOfIntegerNumbers.filter((e) => !arrayOfTwoAbundantSum.includes(e))
							//console.log(res);
						    for(let i = 0;i<res.length;i++){
								sumOfAllNumbers = sumOfAllNumbers + res[i];
							};

							console.log(sumOfAllNumbers);

You’re still dynamically creating the arrays

1 Like