Codility: CountFactors Solution
Lesson 10 Prime and Composite NumbersApproach
If number a
is a divisor of n
, then n/a
is also a divisor.
One of these two divisors is less than or equal to sqrt(n)
.
This could be proven by contradictory. Assume if that were not the case, n
would be a product of two numbers greater than sqrt(n)
, which is impossible.
Implementation
1function solution(N) {2 let result = 03 let i = 14 while (i * i < N) {5 if (N % i === 0) {6 result += 27 }8 i++9 }10 if (i * i === N) {11 result++12 }1314 return result15}
Comments
Loading comments...
Tags
codility
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.
Next Post
Previous Post
Codility: Flags
Lesson 10 Prime and Composite Numbers