Sieve of Eratosthenes - Try 3 - Groovyize





5
Date Submitted Sun. Apr. 29th, 2007 10:04 PM
Revision 1 of 1
Helper esumerfd
Tags "Sieve | Eratosthenes" | groovy | of | primes
Comments 0 comments
Generate prime numbers below some limit. This snippet uses groovy categories to locate the appropriate methods in common classes.
package simulation.sieve.try3;

/*
 * Groovize the algorithm using categories.
 * 1) Add seive method to Integer
 * 3) Add timesFrom method to Integer
 * 4) Add isPrimeFactor to Integer
 * 5) Add toList to Integer to create initial integers
 * 6) Add is prime tracking to Integer
 * 7) Move seive algorigm to List
 */

class Sieve3
{
    def eratosthenes(a_limit)
    {
        def primes
       
        use (IntegerCategory,ListCategory)
        {
            primes = a_limit.seive()
        }

        primes
    }
}
 
/*
 * Add prime related methods to Integer. Use the Integers intenal
 * final value variable to track whether it may be prime or not.
 */

class IntegerCategory
{
    /*
     * Generate list of primes below this number
     */

    def static seive(Integer a_limit)
    {
        def numbers = a_limit.toList()
        numbers.findPrimes()
    }

    /*
     * Generate a list of numbers with this value as the upper bound
     */

    def static toList(Integer a_limit)
    {
        def list = []

        a_limit.times { number ->
            list << number
        }

        list
    }
   
    /*
     * Like times but from a beginning point.
     */

    def static timesFrom(Integer a_limit, Integer a_from, Closure a_closure)
    {
        (a_from..a_limit).each a_closure
    }

    /*
     * Mark the integer as not prime to setting its value to zero
     */

    def static isNotPrime(Integer a_number)
    {
        a_number.value = 0
    }
   
    /*
     * Determine if this number is divisible by the factor. If eliminate
     * the number if it is less than the factor or zero then it can not
     * be prime. Then check for prime by not equal self and not divisible
     * by factor.
     */

    def static hasPrimeFactor(Integer a_number, Integer a_factor)
    {
         (a_number > a_factor
            && a_number.value != 0
              && a_number != a_factor
              && a_number % a_factor == 0)
    }
}

class ListCategory
{
    def static findPrimes(List a_list)
    {
        def primes = []

        a_list.size().timesFrom(2) { factor ->

            a_list.each() { number ->

                // Since this method is in a list now perhaps we should
                // filter out other types in the list
                if (number instanceof Integer)
                {
                    if (number.hasPrimeFactor(factor))
                    {
                        number.isNotPrime()
                    }
                    else if (number == factor)
                    {
                        primes << factor
   
                    }
                }
            }
        }
       
        primes
    }
}


...

 

Edward Sumerfield

Comments

There are currently no comments for this snippet.

Voting

Votes Down