.

Labels

.

Finding prime numbers

script to display prime numbers between zero and given number

prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. A natural number greater than 1 that is not a prime number is called a composite number. For example, 5 is prime, as only 1 and 5 divide it, whereas 6 is composite, since it has the divisors 2 and 3 in addition to 1 and 6. The fundamental theorem of arithmetic establishes the central role of primes in number theory : any integer greater than 1 can be expressed as a product of primes that is unique up to ordering. This theorem requires excluding 1 as a prime.
                                            Wikipediahttp://en.wikipedia.org/wiki/Prime_number )




-------------------------------------------------------------------------------------------------------------------------------------------------
HOW THIS SCRIPT WORKS:
'factor' is a linux command line tool which will display factors of any number.
A prime number will have only 1 and the number itself as it's factors.
-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------

#!/bin/bash

#for displaying prime number between zero and given number

for (( i=2; i<=$1; i++ ));do
l=$(factor $i | cut -d : -f 1)
r=$(factor $i | cut -d : -f 2 | sed 's/ //g')
   if (( $l == $r ));then
       echo $i
   fi
done 

-------------------------------------------------------------------------------------------


In my computer the script is named prime. If you want to find prime numbers between 0 and 25, then type prime 25 in terminal and press enter ...







Scripts to check whether a given number is prime or not.
-------------------------------------------------------------------------------------------------------------------------------------------------
HOW THIS SCRIPT WORKS:
 This script uses 'The Prime Number Trick' given here http://www.murderousmaths.co.uk/games/primcal.htm
-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------
#!/bin/bash

#for checking whether given number is prime or not

check=$(((($1**2)+17)%12))
if [[ $check -eq 6 ]];then
   echo "Yes"
else
   echo "No"
fi

-------------------------------------------------------------------------------------------




-------------------------------------------------------------------------------------------------------------------------------------------------
HOW THIS SCRIPT WORKS:
 This script uses 'The 24 Mystery Method' given here http://www.murderousmaths.co.uk/games/primcal.htm
-------------------------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------------------------
#!/bin/bash

#for checking whether given number is prime or not

check=$(((($1**2)-1)%24))
if [[ $check -eq 0 ]];then
   echo "Yes"
else
   echo "No"
fi


-------------------------------------------------------------------------------------------
Both scripts usage same as the first script


... So is 149 a prime number ??   ; )

Enjoy Linux !!


Don't know what to do with the codes ?? click here




2 comments:

  1. Faster: https://github.com/chadwickboggs/personal-bin-scripts/blob/master/primes

    ReplyDelete
  2. 65 is a prime according to 'The 24 Mystery Method' !?

    ReplyDelete