sea

18th November 2012, 11:30 PM

Heyas

Its been a while since i've done some math,

so out of curiousity and 'fun' i wanted to write a bash script doing that.

AFAIK: A prime number is defined by any number that is only dividable by 1 or itself.

AFAIK: Bash works only with integer numbers, which uses no coma seperation.

Prime Numbers are not EVEN

But 2 beeing the only expection!

So at first a think of a function that helps to me skip every even number (eg 4,8,18,100), to save some 'math power'.

So that function simply divides provided number by 2 and looks if the half number * 2 equals the original number.

I've modified the function so i can pass a 2nd argument which is used when i want to divide the number with something diffrent than 2.

Still thinking that (17/3)*3 is not 17 when counting with int values.

Getting the prime number

So to get the prime numbers i pass every uneven number to check if it by the prime function.

The prime function expects a prime number to be not even as long its dividing number is not 1 or itself.

So it divide the prime counter with every number until current one, and as long it is not even, it expects to be NON prime.

Ok... until i wrote this, i got my script fixed mainwhile., so copy paste from programming to guides & Solutions.

So lets share it for those with homeworks ;)

#!/bin/bash

#

# Description: Simply calculates prime numbers between min and max variable

# Lisence: GPL v3

# Date created: 2012.11.18

# Date changed: 2012.11.18

# Written by: Simon A. Erat, erat . simon æ gmail . com

script_version=0.1

#

# Variables

#

prime_min=3

prime_max=4000

C=$prime_min

#

# Title

#

clear

echo "Calculating prime numbers ($script_version)"

#

# Subs

#

isEven() { # NUM1 [ NUM2]

# Check if number is even, if NUM2 is missing its divideing by 2.

# Returns 0 for success, 1 otherwise.

retval=1

num=$1

[ "" = "$2" ] && div=2 || div=$2

check=$[ $num / $div ]

[ $num -eq $[ $check * $div ] ] && retval=0

return $retval

}

isPrime() { # NUM

# Verifies all numbers from 2 to NUM-1

# If none returns even, it returns 0, 1 otherwise

retval=0

count=2

while [ $count -le $[ $1 - 1 ] ];do

# We already skiped even numbers, so no need to use them now.

if ! isEven $count

then # Like: 15 / 3 = 5 = even

isEven $1 $count && retval=1 && break

fi

((count++))

done

return $retval

}

#

# Display

#

printf "\n\rPrime numbers from $prime_min to $prime_max\n:: Calculating... "

while [ $C -le $prime_max ];do

# Dont need to check even numbers

if ! isEven $C

then # Is it a prime number?

if isPrime $C

then printf " $C"

#else printf "."

fi

fi

((C++))

done

echo

echo

echo "Hope this helped :)"

echo

Hope this helps or you have fun with it :)

Its been a while since i've done some math,

so out of curiousity and 'fun' i wanted to write a bash script doing that.

AFAIK: A prime number is defined by any number that is only dividable by 1 or itself.

AFAIK: Bash works only with integer numbers, which uses no coma seperation.

Prime Numbers are not EVEN

But 2 beeing the only expection!

So at first a think of a function that helps to me skip every even number (eg 4,8,18,100), to save some 'math power'.

So that function simply divides provided number by 2 and looks if the half number * 2 equals the original number.

I've modified the function so i can pass a 2nd argument which is used when i want to divide the number with something diffrent than 2.

Still thinking that (17/3)*3 is not 17 when counting with int values.

Getting the prime number

So to get the prime numbers i pass every uneven number to check if it by the prime function.

The prime function expects a prime number to be not even as long its dividing number is not 1 or itself.

So it divide the prime counter with every number until current one, and as long it is not even, it expects to be NON prime.

Ok... until i wrote this, i got my script fixed mainwhile., so copy paste from programming to guides & Solutions.

So lets share it for those with homeworks ;)

#!/bin/bash

#

# Description: Simply calculates prime numbers between min and max variable

# Lisence: GPL v3

# Date created: 2012.11.18

# Date changed: 2012.11.18

# Written by: Simon A. Erat, erat . simon æ gmail . com

script_version=0.1

#

# Variables

#

prime_min=3

prime_max=4000

C=$prime_min

#

# Title

#

clear

echo "Calculating prime numbers ($script_version)"

#

# Subs

#

isEven() { # NUM1 [ NUM2]

# Check if number is even, if NUM2 is missing its divideing by 2.

# Returns 0 for success, 1 otherwise.

retval=1

num=$1

[ "" = "$2" ] && div=2 || div=$2

check=$[ $num / $div ]

[ $num -eq $[ $check * $div ] ] && retval=0

return $retval

}

isPrime() { # NUM

# Verifies all numbers from 2 to NUM-1

# If none returns even, it returns 0, 1 otherwise

retval=0

count=2

while [ $count -le $[ $1 - 1 ] ];do

# We already skiped even numbers, so no need to use them now.

if ! isEven $count

then # Like: 15 / 3 = 5 = even

isEven $1 $count && retval=1 && break

fi

((count++))

done

return $retval

}

#

# Display

#

printf "\n\rPrime numbers from $prime_min to $prime_max\n:: Calculating... "

while [ $C -le $prime_max ];do

# Dont need to check even numbers

if ! isEven $C

then # Is it a prime number?

if isPrime $C

then printf " $C"

#else printf "."

fi

fi

((C++))

done

echo

echo

echo "Hope this helped :)"

echo

Hope this helps or you have fun with it :)