Hello

For an ebuild I'm working on, I need a function to test wether a number
is a prime number. For that, I wrote an Eclass you find attached to this
e-mail. Can this be commited?

Thanks,
Michael

-- 
Gentoo Linux developer, http://hansmi.ch/, http://forkbomb.ch/
We apologize for the inconvenience, but we'd still like yout to test out
this kernel. 
    -- Linus Torvalds, announcing another kernel patch
# Copyright 1999-2006 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

# Original Author: Michael Hanselmann <[EMAIL PROTECTED]>
# Purpose: Functions for prime numbers

ECLASS="prime"
INHERITED="$INHERITED $ECLASS"
EXPORT_FUNCTIONS primes is_prime

# Prints a list of primes between min and max inclusive
#
# Note: this functions gets very slow when used with large numbers.
#
# Syntax: primes <min> <max>
primes() {
        local min=${1} max=${2}
        local result= primelist=2 i p

        [[ ${min} -le 2 ]] && result="${result} 2"

        for ((i = 3; i <= max; i += 2))
        do
                for p in ${primelist}
                do
                        [[ $[i % p] == 0 || $[p * p] -gt ${i} ]] && \
                                break
                done
                if [[ $[i % p] != 0 ]]
                then
                        primelist="${primelist} ${i}"
                        [[ ${i} -ge ${min} ]] && \
                                result="${result} ${i}"
                fi
        done

        echo ${result}
}

# Checks wether a number is a prime number
#
# Syntax: is_prime <number>
is_prime() {
        local number=${1} i
        for i in $(primes ${number} ${number})
        do
                [[ ${i} == ${number} ]] && return 0
        done
        return 1
}

Attachment: pgpkYeUBO4UOa.pgp
Description: PGP signature

Reply via email to