cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Visit the PTCooler (the community lounge) to get to know your fellow community members and check out some of Dale's Friday Humor posts! X

ISPrime Issues

ppal
17-Peridot

ISPrime Issues

Hi 

 

Issue with file as below. Prime 7 attached. I can't allocate the values to "Data" without doing the symbolic evaluation.

 

ppal_0-1648005064431.png

 

ACCEPTED SOLUTION

Accepted Solutions

Contrary to Prime,  in (real) Mathcad you can make a numeric function IsPrime from the symbolic isprime:

LucMeekes_0-1648044033893.png

LucMeekes_1-1648044084455.png

So the function proposed by the OP is possible:

LucMeekes_2-1648044118482.png

and gives:

LucMeekes_3-1648044140401.png

There's another function: nextprime that can also be helpful here:

LucMeekes_4-1648044518147.png

LucMeekes_5-1648044551774.png

Success!
Luc

View solution in original post

13 REPLIES 13

Hi,

isPrime() is a symbolic function that can only be used symbolically hence the error message if you try to evaluate it numerically.

Capture.JPG

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:

LucMeekes_0-1648044033893.png

LucMeekes_1-1648044084455.png

So the function proposed by the OP is possible:

LucMeekes_2-1648044118482.png

and gives:

LucMeekes_3-1648044140401.png

There's another function: nextprime that can also be helpful here:

LucMeekes_4-1648044518147.png

LucMeekes_5-1648044551774.png

Success!
Luc

Werner_E
25-Diamond I
(To:ppal)

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.

Werner_E_0-1648045405915.png

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.

Werner_E_1-1648044576255.png

 

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).

StuartBruff
23-Emerald III
(To:ppal)

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.

 

2022 04 24 a.png

 

2022 04 24 b.png

 

2022 04 24 c.png

 

2022 04 24 d.png

 

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

Werner_E
25-Diamond I
(To:StuartBruff)

Remarkable work as usual!

Find attached a Prime 6 version of your file.

 

StuartBruff
23-Emerald III
(To:Werner_E)

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

Werner_E
25-Diamond I
(To:StuartBruff)


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!

LucMeekes
23-Emerald III
(To:StuartBruff)

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

LucMeekes
23-Emerald III
(To:LucMeekes)

And another, albeit small, speed improvement.

 

Werner_E
25-Diamond I
(To:LucMeekes)

Here is a programmed sieve (so it won't work in Express) done quite some time ago..

Werner_E_0-1648379780713.png

It looks like the many calls to individual functions in EratosthenesO (necessary to do so in PrimeExpress) are quite time consuming:

Werner_E_0-1648382656118.png

 

 

LucMeekes
23-Emerald III
(To:Werner_E)

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.

LucMeekes_0-1648395100627.png

LucMeekes_1-1648395421418.png

But this still uses the full range of n. I'm trying to get it down to just the odd numbers.

 

Luc

Werner_E
25-Diamond I
(To:LucMeekes)

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 😉

 

ppal
17-Peridot
(To:LucMeekes)

Now easier to solve https://converged.yt/primel/

Announcements

Top Tags