Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Better solution for finding numbers with exactly 3 divisors

I was studying some programming and I found an exercise to write an algorithm finding "threesome numbers" (numbers that are divisible by exactly 3 numbers). I wrote this:

function threesomeNumber(N) {     var found = 0;     var i = 1;     var numberOfDivisions = 1;     while (found < N) {         for (var j = 2; j <= i; j++) {             if (i % j === 0) {                 numberOfDivisions++;             }         }         if (numberOfDivisions === 3) {             found++;             console.log(found + " = " + i);         }         numberOfDivisions = 1;         i++;     } } 

The problem is it's running kinda slow and I was wondering if it can be done quicker. Does anybody know of a more optimized solution? I want it to find N consecutive threesome numbers.

like image 537
Chris Kraszewski Avatar asked Dec 08 '15 18:12

Chris Kraszewski


People also ask

How do you find if a number has exactly 3 divisors?

Given a number N, print all numbers in the range from 1 to N having exactly 3 divisors. Examples: Input : N = 16 Output : 4 9 4 and 9 have exactly three divisors. Divisor Input : N = 49 Output : 4 9 25 49 4, 9, 25 and 49 have exactly three divisors.

Which are the numbers that have exactly three divisors?

Which are the numbers between 1 and 100 that have exactly three factors? 4, 9, 25, 49.

How do you find the divisors of a number efficiently?

The most basic method for computing divisors is exhaustive trial division. If we want to find the positive divisors for an integer n, we just take the integers 1, 2, 3, . . . , n, divide n by each, and those that divide evenly make up the set of positive divisors for n.

What has exactly 3 positive divisors?

The square of a prime number has 3 divisors : 1, that prime number, and the square of that prime number.

How do you find the number with 3 positive divisors?

Three Divisors Given an integer n, return true if n has exactly three positive divisors. Otherwise, return false. An integer m is a divisor of n if there exists an integer k such that n = k * m. Input: n = 2 Output: false Explantion: 2 has only two divisors: 1 and 2. Input: n = 4 Output: true Explantion: 4 has three divisors: 1, 2, and 4.

How to print all numbers from 1 to n having exactly 3 divisors?

To print all numbers in the range from 1 to N having exactly 3 divisors, the main calculation is to find those which are squares of prime numbers and less than or equal to the number. We can do this as follows: Start a loop for integer i from 2 to n. Check if i is prime or not, which can be done easily using the isPrime (n) method.

How to find all prime numbers with exactly 3 divisors?

Given a number N, return a count of numbers till N (N included) which have exactly 3 divisors. Approach - We need to find only prime numbers until sqrt (N).Let x be such a number found so x*x would have only 1,x*x, and x as its divisor.

How to count the number with exactly 3 divisors in a list?

Your task is to complete the function threeDivisors () which takes an integer q and a list of integer of size q as input parameter and returns the list containing the count of the numbers having exactly 3 divisors for each query. We are replacing the old Disqus forum with the new Discussions section given below.


2 Answers

The only threesome numbers are squares of primes (divisors 1, p, p^2). Just do Erathostenes and return the squares.

Proof: If it has an odd number of divisors it is known to be a square. Since 1 and n^2 are always divisors of n^2, we may only have one more divisor, i.e. n. Any divisor of n would be another divisor of n^2, therefore n is prime.

Example (based on given code):

function threesomeNumber(N) { var found = 0; var i = 2; var prime = true; while (found < N) {     // Naive prime test, highly inefficient     for (var j = 2; j*j <= i; j++) {         if (i % j === 0) {             prime = false;         }     }     if (prime) {         found++;         console.log(found + " = " + (i*i));     }     prime = true;     i++;   } } 
like image 61
Hermann Döppes Avatar answered Sep 18 '22 21:09

Hermann Döppes


You could implement an algorithm based on the sieve of Eratosthenes. The only change is that you don't mark the multiples of found primes, but the multiples of found numbers which have at least 3 divisors. The reason is that you can be sure that the multiples of these numbers have more than 3 divisors.

EDIT: Hermann's solution is the best for "threesomes". My idea is more general and applicable for "N-somes".

like image 23
Andrea Dusza Avatar answered Sep 20 '22 21:09

Andrea Dusza