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.
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 between 1 and 100 that have exactly three factors? 4, 9, 25, 49.
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.
The square of a prime number has 3 divisors : 1, that prime number, and the square of that prime number.
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.
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.
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.
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.
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++; } }
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".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With