Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Factors of a number

/*
	JavaScript program for factors of a number by lolman_ks.
    Same logic can be used to build this program in other languages.
*/
/*
	Logic: Divide the number by every number less than half of the number and 
    check for exact divisibility.
    Add to the factors array if exactly divisible.
*/

function factors(number){
  	var factor_array = []; //Create an empty array. 
  	//Run a loop to check divisibility.
	for(let i = 0;i < Math.ceil(number / 2);++i){
    	if(number % i == 0) factor_array.push(i); //Check for exact divisibility.
    }
 	factor_array.push(number); //Add the number itself in the factor array.
  	return factor_array;
}

//Program for perfect number can also be built using the above function.

function perfect_number(number){
	const factor_array = factors(number);
  	var sum = 0;
  	for(let i = 0;i < factor_array.length;++i){
    	sum += factor_array[i];
    }  
  	if(sum == number * 2) return true;
  	else return false;
}

/*
	I hope that my answers are useful to you. Please promote them if they are.
    #lolman_ks.
*/
Comment

factors of a number

def factors(num):
    """Get factors for given integer."""
    result = set()
    for i in range(1, int(num ** 0.5) + 1):
        if num % i == 0:
            result.update({i, num // i})
    return result
Comment

Number Of Factors

function countFactors(num)
{
var finalEnd = num/2;
var count = 1;

for(var i=1; i<=finalEnd; i++)
{
if(num%i==0)
{
count++;
}

}
return count;

}


console.log(countFactors(24));
Comment

PREVIOUS NEXT
Code Example
Javascript :: remove decimal places js 
Javascript :: axios response error interceptor 
Javascript :: javascript last value of array 
Javascript :: fs 
Javascript :: or js 
Javascript :: mounting in react 
Javascript :: dom traversal jquery 
Javascript :: how to convert string into int js 
Javascript :: filesaver.js cdn 
Javascript :: regex in javascript 
Javascript :: open json javascript 
Javascript :: Remove uploaded file in jquery 
Javascript :: react component pass props 
Javascript :: how to focus out of an input in testing library 
Javascript :: how to convert string to reverse title case in javascript 
Javascript :: how to save data in javascript 
Javascript :: javascript prevent more than one click 
Javascript :: truthy or falsy 
Javascript :: javascript arrow function syntax 
Javascript :: JavaScript pauses the async function until the promise 
Javascript :: JavaScript HTML DOM Navigation 
Javascript :: jquery callback functions 
Javascript :: bootstrap on tabs change 
Javascript :: javascript read all cookies 
Javascript :: phaser random triangle 
Javascript :: Counting Duplicates 
Javascript :: npm deploy next js with tailwind 
Javascript :: how to change name on tab when user goes to another tab 
Javascript :: Use Prototype To Add A Property To Javascript Class 
Javascript :: ... in javascript 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =