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

# The-F00L's solution

## to Nth Prime in the CFML Track

Published at Apr 13 2021 · 0 comments
Instructions
Test suite
Solution

Given a number n, determine what the nth prime is.

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

If your language provides methods in the standard library to deal with prime numbers, pretend they don't exist and implement them yourself.

To run the code in this exercise, you will only need to have CommandBox CLI installed. This binary runs CFML code from the command line.

To run the tests, `cd` into the exercise folder and run the following:

``````box task run TestRunner
# Or start up a test watcher that will rerun when files change
``````

The tests leverage a library called TestBox which supports xUnit and BDD style of testing. All test suites will be written in the BDD style which uses closures to define test specs. You won't need to worry about installing TestBox. The CLI test runner will take care of that for you. You just need to be connected to the internet the first time you run it. You can read more about it here:

https://testbox.ortusbooks.com/content/

## Source

A variation on Problem 7 at Project Euler http://projecteuler.net/problem=7

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### NthPrimeTest.cfc

``````component extends="testbox.system.BaseSpec" {

function beforeAll(){
SUT = createObject( 'NthPrime' );
}

function run(){

describe( "My NthPrime class", function(){

it( 'first prime', function(){
expect( SUT.prime( number='1' ) ).toBe( '2' );
});

it( 'second prime', function(){
expect( SUT.prime( number='2' ) ).toBe( '3' );
});

it( 'sixth prime', function(){
expect( SUT.prime( number='6' ) ).toBe( '13' );
});

it( 'big prime', function(){
expect( SUT.prime( number='10001' ) ).toBe( '104743' );
});

it( 'there is no zeroth prime', function(){
expect( function(){ SUT.prime( number='0' ); } ).toThrow( message='there is no zeroth prime' );
});

});

}

}``````

### SolutionTest.cfc

``````component extends="NthPrimeTest" {

function beforeAll(){
SUT = createObject( 'Solution' );
}

}``````
``````/**
* Your implmentation of the NthPrime exercise
*/
component {

/**
* @returns
*/
function prime( number ) {
if (lsParseNumber(number)==0) {
throw('there is no zeroth prime' );
}
ctr=0;
dvr=2;
while (ctr<lsParseNumber(number)) {
if (isPrime(dvr)) {
ctr+=1;
}
dvr+=1;
}
return dvr-=1;
}

function isPrime( number ) {
for (i = 2; i < number; i++) {
if (number%i==0){
return false;
}
}
return true;
}

}``````

### What can you learn from this solution?

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?