Results 1 to 10 of 13
I wrote this script to find whether the given number is prime or not. Logically its correct but it not working so can anyone please find a solution to this ...
- 05-11-2009 #1Just Joined!
- Join Date
- May 2009
- Posts
- 4
Need correction of prime number script
I wrote this script to find whether the given number is prime or not. Logically its correct but it not working so can anyone please find a solution to this problem.
Thank you.Code:clear echo "Application to Find if the given number is prime or not" echo "Enter the number" read num prime = `expr $num % 2` if [ $num -eq 2 ] then echo "The given number $num is prime number" exit elif [ $prime -eq 1 ] then echo "The given number $num is prime number" else echo "The given number $num is not a prime number" fi
- 05-12-2009 #2Linux Newbie
- Join Date
- Dec 2006
- Posts
- 119
i recoded your code and here is the correct script.
i am not going to explain it but i hope that it helps you. i think that you could figure out.
let me know if you want me to explain it why your version wasn't working.
Code:#!/bin/bash clear echo "Application to Find if the given number is prime or not" echo "Enter the number" read num prime=`expr $num % 2` if [ $prime -eq 1 ]; then echo "The given number $num is not a prime number" else echo "The given number $num is prime number" fi
- 05-12-2009 #3Just Joined!
- Join Date
- May 2009
- Posts
- 4
Thanks
Thank you slimx3m. Yes, Can you please let me know why my version was not working ?
- 05-12-2009 #4Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
The corrected script will show if the number is divisible by two, but neither will determine if the number is prime. You need something to emulate the Sieve of Aristhostenes (a prime number sieve). The algorithm is available in many math text books, as well as math reference books such as "The VNR Concise Enclyclopedia of Mathematics". My copy is well-used and has held a prominent place on my technical book shelf for over 20 years. You also might want to set a limit on the input number as very large numbers that are not divisible by 2 (anything ending in an even number) can take awile to determine whether they are prime.
Hint. The sieve is an algorithm that is well served by a recursive process. With a 100 number starting segment, a number up to 100,000,000 will only take 2 recursions. 10,000,000,000,000,000 (16 digits) only 3. FYI, that is about the limit of a double-precision floating point number. A 64 bit unsigned integer can handle up to between 18 and 19 digits. For numbers bigger than that you will need an unlimited precision math package, which are a lot slower than native integers. Extended 80 bit precision on the x86/x87 processor can allow up to 23-24 digits, but it requires specialized programming to directly access the math co-processor unit of the Pentium chip.Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 05-12-2009 #5
The problem was with this line:
Bash doesn't like those spaces.Code:prime = `expr $num % 2`
Neither of these scripts will tell you if a given number is prime or not, though. They really only tell you if the number is odd or even. There is no definitive algorithm for determining primes (yet). If there was, much of the cryptography that's used today would be obsolete. However, there are more complicated algorithms for determining the primality of a number including Fermat's test and the more robust Miller-Rabin test.
- 05-12-2009 #6Linux Newbie
- Join Date
- Dec 2006
- Posts
- 119
sharath88,
i guessed that your question has been answered pretty well by rubberman and thrillhouse.
- 05-12-2009 #7Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
Actually, determining if a number is prime or not isn't difficult. However, the time to determine it goes up logarithmically with the length of the number, which is one reason why public key encryption algorithms use keys upwards of 1024 digits (not bits). The other reason why it is hard to break PKI encrypted data is that they key is a the product of two 1024+ digit prime numbers, and factorizing such numbers is even harder than determining if the number is prime or not. However, the Sieve of Eratosthenes (I misspelled it below) will determine if a specific number is prime fairly efficiently, and accurately. There are other, more modern algorithms as well, especially for factoring large numbers into their prime factors, which is what is required for breaking PKI encryption. An old such algorithm is called Euclid's algorithm, which given that Euclid discovered it, is quite old!
Both of these fellows were ancient Greek mathematicians, though not quite contemporaries. Euclid lived around 300 B.C. and Eratosthenes around 275-200 B.C. (give or take). So, they had a handle on this stuff well over 2000 years ago!
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 05-12-2009 #8For large numbers, primality tests can only tell you if a number is probably prime, depending on the degree of certainty you need. There still exists composite numbers that can pass any primality test, Carmichael numbers in the case of Fermat's test. For this script, the algorithm chosen (besides mod'ing by 2) to determine primality is trivial considering the largest number that will fit inside of a primitive int is very small in the cryptographic sense. But that was really besides the point.Actually, determining if a number is prime or not isn't difficult. However, the time to determine it goes up logarithmically with the length of the number
This is true for RSA but not all public key encryption algorithms.The other reason why it is hard to break PKI encrypted data is that they key is a the product of two 1024+ digit prime numbers, and factorizing such numbers is even harder than determining if the number is prime or not.
None of the algorithms (to date) for factoring large numbers into their prime composites are fast enough for very large numbers. If you were to use Euclid (or even Extended Euclid) to crack a 1024-bit RSA key, you would be waiting for a very long time. But if you can predict the distribution of primes (i.e. prove the Riemann Hypothesis) then it doesn't matter which integer factorization algorithm you choose. But this is all beyond the scope of the original poster's question.However, the Sieve of Eratosthenes (I misspelled it below) will determine if a specific number is prime fairly efficiently, and accurately. There are other, more modern algorithms as well, especially for factoring large numbers into their prime factors, which is what is required for breaking PKI encryption. An old such algorithm is called Euclid's algorithm, which given that Euclid discovered it, is quite old! Both of these fellows were ancient Greek mathematicians, though not quite contemporaries. Euclid lived around 300 B.C. and Eratosthenes around 275-200 B.C. (give or take). So, they had a handle on this stuff well over 2000 years ago!
- 05-12-2009 #9Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
So, in the final analysis (if there ever is one), sharath88 needs to incorporate something more than determining non-divisibility by two for this script. It is certainly possible, and building or hard-coding a 100 cell segment for a sieve would enable him to have a reasonable prime number detection tool for smallish numbers (up to 100M).
Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 05-15-2009 #10Just Joined!
- Join Date
- May 2009
- Posts
- 4
Thank you all for your answer. I understood the problem.


Reply With Quote
