Skip to content

Instantly share code, notes, and snippets.

@sapanparikh18
Created October 24, 2018 08:03
Show Gist options
  • Save sapanparikh18/4dab64448bb664514a7e6896390664fb to your computer and use it in GitHub Desktop.
Save sapanparikh18/4dab64448bb664514a7e6896390664fb to your computer and use it in GitHub Desktop.
Created with Copy to Gist
package primefactors
import spock.lang.Specification
class PrimeFactorsSpec extends Specification{
def "api to t test nothing"(){
expect: "nothing" == "nothing"
}
def "API to test prime factors of given integer"(){
expect: "factorsOf(n) will return list with n's prime factors"
factorsOf(n) == lst
where:
n || lst
1 || []
2 || [2]
3 || [3]
4 || [2,2]
5 || [5]
6 || [2,3]
7 || [7]
8 || [2,2,2]
9 || [3,3]
2*2*3*3*7*11*11*13 || [2,2,3,3,7,11,11,13]
36 || [2,2,3,3]
}
List<Integer> factorsOf(int n){
List<Integer> factors = []
def divisor = 2
while(n>1) {
while(n%divisor == 0){
factors.add(divisor)
n /= divisor
}
divisor++
}
return factors
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment