Codility: CountFactors Solution

Lesson 10 Prime and Composite Numbers

Approach

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 = 0
3 let i = 1
4 while (i * i < N) {
5 if (N % i === 0) {
6 result += 2
7 }
8 i++
9 }
10 if (i * i === N) {
11 result++
12 }
13
14 return result
15}

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

Hackerrank: Frequency Queries

Jan 27, 2021

Previous Post

Codility: Flags

Jan 26, 2021

Lesson 10 Prime and Composite Numbers

HoningJS

Search Posts