# Scala algorithm: Count factors/divisors of an integer

Published

## Algorithm goal

A number $$y$$ is a factor of $$x$$ if $$x$$ is divisible by $$y$$. Find the number of distinct factors of a number $$x$$.

For example, 2 has two factors: $$1$$ and $$2$$. 16 has 5 factors: $$1$$, $$2$$, $$4$$, $$8$$, and $$16$$.

This problem is similar to the codility problem CountFactors - Count factors of given number n.

 Total count Divides 16? 5 Factor count so far 1 2 3 4 5 6 7 8 9 â€¦ 15 16 âœ“ âœ“ âœ— âœ“ âœ— âœ— âœ— âœ“ âœ— âœ— âœ— âœ“ 1 2 2 3 3 3 3 4 4 4 4 5

## Test cases in Scala

assert(countFactors(1) == 1)
assert(countFactors(2) == 2)
assert(countFactors(3) == 2)
assert(countFactors(4) == 3)
assert(countFactors(5) == 2)
assert(countFactors(6) == 4)
assert(countFactors(16) == 5)
assert(countFactors(24) == 8)
assert(countFactors(36) == 9)
assert(countFactors(Int.MaxValue) == 2)


## Algorithm in Scala

8 lines of Scala (compatible versions 2.13 & 3.0), showing how concise Scala can be!

## Explanation

In a brute-force approach, for number $$n$$, we can check for all numbers that are divisible, up to $$n$$.

However, there is a more efficient approach, in particular if we consider that for every factor that is under $$\sqrt{n}$$, there a corresponding factor to be counted that is above $$\sqrt{n}$$, meaning every divisor under the square root has a corresponding divisor above it - 2 divisors. (this is Â© from www.scala-algorithms.com)

## Scala concepts & Hints

1. ### Collect

'collect' allows you to use Pattern Matching, to filter and map items.

assert("Hello World".collect {
case character if Character.isUpperCase(character) => character.toLower
} == "hw")

2. ### Pattern Matching

Pattern matching in Scala lets you quickly identify what you are looking for in a data, and also extract it.

assert("Hello World".collect {
case character if Character.isUpperCase(character) => character.toLower
} == "hw")

3. ### Range

The (1 to n) syntax produces a "Range" which is a representation of a sequence of numbers.

assert((1 to 5).toString == "Range 1 to 5")

assert((1 to 5).reverse.toString() == "Range 5 to 1 by -1")

assert((1 to 5).toList == List(1, 2, 3, 4, 5))

4. ### View

The .view syntax creates a structure that mirrors another structure, until "forced" by an eager operation like .toList, .foreach, .forall, .count.

# Scala Algorithms: The most comprehensive library of algorithms in standard pure-functional Scala

## How our 100 algorithms look

1. A description/goal of the algorithm.
2. An explanation with both Scala and logical parts.
3. A proof or a derivation, where appropriate.
4. Links to Scala concepts used in this specific algorithm, also unit-tested.
5. An implementation in pure-functional immutable Scala, with efficiency in mind (for most algorithms, this is for paid subscribers only).
6. Unit tests, with a button to run them immediately in our in-browser IDE.

### Study our 100 Scala Algorithms: 6 fully free, 100 published & 0 upcoming

Fully unit-tested, with explanations and relevant concepts; new algorithms published about once a week.

### Explore the 22 most useful Scala concepts

To save you going through various tutorials, we cherry-picked the most useful Scala concepts in a consistent form.

## Subscribe to Scala Algorithms

Maximize your Scala with disciplined and consistently unit-tested solutions to 100+ algorithms.

Use it from improving your day-to-day data structures and Scala; all the way to interviewing.