🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of damiankorz

damiankorz's solution

to Nth Prime in the CFML Track

Published at Dec 26 2018 · 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
box task run TestRunner --:watcher

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' );
	}

}
component {
	/**
	* @returns
	*/
	public numeric function prime( required numeric number ) {
		var factors = 0;
		var primes = [];
		var count = number;
		var currentNum = 2;
		if( number == 0 ){
			throw('There is no zeroth prime' );
		}
		while ( count > 0 ){
			for( var i = 1; i <= currentNum; i++ ){
				if( currentNum % i == 0 ){
					factors += 1;
				}
			}
			if( factors <= 2 ){
				arrayAppend( primes, currentNum );
				count -= 1;
			}
			currentNum += 1;
			factors = 0;
		}
		return primes[number];
	}
}

damiankorz's Reflection

May not be optimal solution. First four test cases pass. Unable to handle 'big prime' test.