1/31/2018
Posted by 
Prime Number Program In Tcl Rating: 4,4/5 6820votes
Prime Number Program In Tcl

Prime number program in C++ Prime number is natural number which is greater than 1 and only divisible by 1 or itself. For example, 6 is not prime number because it is. Print all prime numbers in range. One problem is how to discover if a particular number is a prime number. Now all you need to do is convert the above to Tcl. These sample math programs are REALLY simple. A program that computes the 'osculator' for a prime number and uses it to.% tclsh83 prime_power.tcl Enter.

>>>>I am trying to write a program that will figure out if a number is prime >>or not. Currently this is the code that I have: >>[code cut] >>Hi Andrade1, >>As in your other previous homework programs, you seem to have some trouble >with control flow. Have you considered talking with your professor or >teaching assistant about this? >>>>import math >>>>def main(): >>number=input('Please enter a positive whole number greater than 2: >>') >>for value in range(2, number-math.sqrt): >>if number% value == 0: >>print number, 'is not prime' >>exit >>print number, 'is prime' >>>>main() >>>>my problem is that the output prints both statements whether the number >>is prime or not.

>>>The block of code: >>###### >for value in range(2, number-math.sqrt): >if number% value == 0: >print number, 'is not prime' >exit >###### >>has two issues. What is 'exit'? Instead of exit I can use break so that the loop will stop executing >2. What is 'number - math.sqrt'? For math.sqrt I could use math.sqrt(n) so that it would take the sqrt of n which is entered in by the user However for the output I am still receiving enter a number greater than 2: 49 49 is prime 49 is prime 49 is prime 49 is prime 49 is prime 49 is not prime >Instead of 'exit', you may be looking for the 'return' statement instead. >>>Good luck to you.

>>Recent Messages in this Thread. Kundli Pro Software For Windows Xp Full Version there.

Finding prime numbers is something that is pretty well explained out there, so I'll explain a bit more about how you should be thinking in order to work out the solution. Like that, I hope you'll be better able to answer such problems yourself in the future. To start with, you've got two problems here. One problem is how to discover if a particular number is a prime number, and another problem is how to find all the prime numbers in a given range.

These two are indeed linked: we can use the solution to one to solve the other. Let's start by doing that. (This is pseudocode, not Tcl!) # Start at 2; 1 is defined to be a non-prime for every i in 2 up to 100 if (isPrime i) print i, ' is prime' else print i, ' is not prime' end if end for Next, we need a mechanism for that isPrime. It's something that's best written as a named subprogram (a procedure in Tcl). We'll use the simplest technique here, a primality test by simple-minded trial division. Function isPrime (integer x): boolean # Note, when x is 2, this loop does *zero* steps for every i in 2 up to x-1 if (x mod i = 0) # Early exit from function; we know the answer to do more work! Return false end if end for return true end function That's not efficient (you can stop earlier, you can keep a cache of what smaller primes have already been found and only check against those, etc.) but it will work.