Community Tip - Your Friends List is a way to easily have access to the community members that you interact with the most! X
Hi
Issue with file as below. Prime 7 attached. I can't allocate the values to "Data" without doing the symbolic evaluation.
Solved! Go to Solution.
Contrary to Prime, in (real) Mathcad you can make a numeric function IsPrime from the symbolic isprime:
So the function proposed by the OP is possible:
and gives:
There's another function: nextprime that can also be helpful here:
Success!
Luc
Hi,
isPrime() is a symbolic function that can only be used symbolically hence the error message if you try to evaluate it numerically.
In mathematics, the sieve of Eratosthenes is an ancient algorithm for finding all prime numbers up to any given limit.
Check this in Wikipedia.
Cheers
Terry
Contrary to Prime, in (real) Mathcad you can make a numeric function IsPrime from the symbolic isprime:
So the function proposed by the OP is possible:
and gives:
There's another function: nextprime that can also be helpful here:
Success!
Luc
As Terry already wrote, IsPrime() is a symbolic only function and so you can't use is with numeric evaluations as you already had experienced. In real Mathcad (MC15 and below) there were tricky ways to pack symbolic evals in a program and so use a symbolic-only function in numeric evaluations, too. But the method wasn't 100% clean and reliable (you may look up the many threads about the LambertW function here in the forum). In Prime those tricks are not working anymore and so I see no way you could use a symbolic-only function with numeric evalution (after all thats what symbolic-only means anyway 😉
So all you can do is do write your own isPrime function. You may create a custom function as described in help here https://support.ptc.com/help/mathcad/r6.0/en/index.html#page/PTC_Mathcad_Help%2Fabout_custom_functions.html
or use Primes programming ability - the latter of course will be working much slower but may be easier to accomplish and share with others.
Here is a basic example of a isPrime function which then is used to create a nextPrime function.
Keep in mind that with numeric evaluation you can't use it for integers with more than 15 digits. The function will fail/give wrong results as you can see in the next screenshot. You could evaluate the function symbolically, too, but it will be very slow so for symbolic eval its sure better to stick with the built-in symbolic-only function.
Your example shows a program which returns all primes up to a given number. You may do without the "x", simply write
if IsPrime(i) .... (no need for =1, too).
But as Terry already wrote, for the task of calculating a list of the first primes, Eratosthenes is sure more efficient.
BTW, this also could be a way to speed-up the isPrime() function: Precalculate all Primes up to 10^6 on top of the worksheet first. Then isPrime could use that list instead of trying every odd number from 3 to the square root. This should make isPrime() work much faster and would accommodate numbers up to 10^12 (=(10^6)^2).
For those of you with only Prime Express, here are a couple of methods of generating prime numbers. I wrote it in Mathcad Prime Express 7, but the attached version is in Mathcad Prime 8 ... unfortunately, I somewhat unthinkingly overwrote the Prime 7 version.
I had a need for some prime numbers but didn't want to spend too much time creating the methods, so it's a bit rough and ready. I'm confident that there are better and more efficient ways to generate primes in Mathcad Express. It takes a looong time to generate a million primes!
Stuart
Remarkable work as usual!
Find attached a Prime 6 version of your file.
After scanning my worksheet library, I realized that I've posted this Express prime generator before, albeit as part of another worksheet.
https://community.ptc.com/t5/PTC-Mathcad/Pi-and-Prime-numbers/td-p/677039/highlight/true
I keep telling myself that I'm going to go through all my worksheets and create a list of functions that I've written. But that day is not today, as I've got some serious procrastinating to do that won't wait until tomorrow ...
Stuart
But that day is not today, as I've got some serious procrastinating to do that won't wait until tomorrow ...
Oh well, I know exactly what you are talking about!
Here's a solution that does not run into the recursion limit, and is a bit speedier. Generates primes up to one million within 100 s.
Success!
Luc
Here is a programmed sieve (so it won't work in Express) done quite some time ago..
It looks like the many calls to individual functions in EratosthenesO (necessary to do so in PrimeExpress) are quite time consuming:
Clever, I didn't think of using the index as the number. That speeds things up considerably, because it saves me from using the Stacknz() function. I can use match() instead.
Where n=10^7 would take 15 minutes (my PC is a little faster than yours), it's now done within a minute.
But this still uses the full range of n. I'm trying to get it down to just the odd numbers.
Luc
I am not surprised that my machine is slower than yours, after all its pretty old 😉
Going down to just the odd numbers was the reason for those awkward start and second values in the j-loop to "strike through" all multiples of the prime.
We can start with the striking of all odd(!) multiples of the prime p with index i (p=2*i+3) with p^2 as all other multiples before already should be treated. p^2 corresponds to index (p^2 -3)/2 = ((2*i+3)^2-3)/2=2*i^2 + 6*i +3. Similar for the second value in the j-loop.
I spent the last minutes to think what I had done back then to come up with those values - I had forgotten but I knew it was a bit tricky 😉