ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

Published at Apr 16 2019
·
0 comments

Instructions

Test suite

Solution

Compute the prime factors of a given natural number.

A prime number is only evenly divisible by itself and 1.

Note that 1 is not a prime number.

What are the prime factors of 60?

- Our first divisor is 2. 2 goes into 60, leaving 30.
- 2 goes into 30, leaving 15.
- 2 doesn't go cleanly into 15. So let's move on to our next divisor, 3.

- 3 goes cleanly into 15, leaving 5.
- 3 does not go cleanly into 5. The next possible factor is 4.
- 4 does not go cleanly into 5. The next possible factor is 5.

- 5 does go cleanly into 5.
- We're left only with 1, so now, we're done.

Our successful divisors in that computation represent the list of prime factors of 60: 2, 2, 3, and 5.

You can check this yourself:

- 2 * 2 * 3 * 5
- = 4 * 15
- = 60
- Success!

To run the tests, run the command `dotnet test`

from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time.
Once you get the first test passing, remove the `Skip`

property from the next test and work on getting that test passing.
Once none of the tests are skipped and they are all passing, you can submit your solution
using `exercism submit PrimeFactors.cs`

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

The Prime Factors Kata by Uncle Bob http://butunclebob.com/ArticleS.UncleBob.ThePrimeFactorsKata

```
// This file was auto-generated based on version 1.1.0 of the canonical data.
using Xunit;
public class PrimeFactorsTest
{
[Fact]
public void No_factors()
{
Assert.Empty(PrimeFactors.Factors(1));
}
[Fact(Skip = "Remove to run test")]
public void Prime_number()
{
Assert.Equal(new[] { 2 }, PrimeFactors.Factors(2));
}
[Fact(Skip = "Remove to run test")]
public void Square_of_a_prime()
{
Assert.Equal(new[] { 3, 3 }, PrimeFactors.Factors(9));
}
[Fact(Skip = "Remove to run test")]
public void Cube_of_a_prime()
{
Assert.Equal(new[] { 2, 2, 2 }, PrimeFactors.Factors(8));
}
[Fact(Skip = "Remove to run test")]
public void Product_of_primes_and_non_primes()
{
Assert.Equal(new[] { 2, 2, 3 }, PrimeFactors.Factors(12));
}
[Fact(Skip = "Remove to run test")]
public void Product_of_primes()
{
Assert.Equal(new[] { 5, 17, 23, 461 }, PrimeFactors.Factors(901255));
}
[Fact(Skip = "Remove to run test")]
public void Factors_include_a_large_prime()
{
Assert.Equal(new[] { 11, 9539, 894119 }, PrimeFactors.Factors(93819012551));
}
}
```

```
using System;
using System.Collections.Generic;
public static class PrimeFactors
{
public static int[] Factors(long number)
{
var list = new List<int>();
int division = 2;
do
{
if(number % division == 0)
{
list.Add(division);
number = number / division;
division = 2;
}
else division++;
}
while (number > 1);
return list.ToArray();
}
}
```

A huge amount can be learned from reading other peopleâ€™s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

- What compromises have been made?
- Are there new concepts here that you could read more about to improve your understanding?

Level up your programming skills with 3,449 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors.
Exercism is
**100% free forever**.

## Community comments