The Museum of HP Calculators

HP Forum Archive 21

[ Return to Index | Top of Index ]

WP-34S (Prime Number Test) question
Message #1 Posted by Barry Mead on 20 Oct 2013, 2:45 p.m.

I looked into how the Prime? test works, and essentially it takes the integer part of the X register IP(X) then tests that value for primality. If instead it took the RND(X) before doing the primality test then numbers with tiny rounding errors would still accurately indicate the expected primality.

For example if you take the number 79 and divide it by 3 then multiply it by 3, you get a number with a tiny rounding error in the 16th digit. On the display (even fix 11, or all 11) it appears to be 79 exactly. If you display the whole number with (f <) it shows 78.99999999999999.

So the primality test (h test prime?) clips it down to 78 and shows false. If the RND(X) were applied to the number prior to testing for primality, then the result will always reflect the primality of the number shown on the display. What do you think?

Edited: 20 Oct 2013, 3:06 p.m.

      
Re: WP-34S (Prime Number Test) question
Message #2 Posted by Dieter on 20 Oct 2013, 3:13 p.m.,
in response to message #1 by Barry Mead

All numerical functions always work with the exact, unrounded argument in (usually) X. And I think it's fine this way.

Consider your example with the 78,99999999999999 that is displayed as 79 since you do not see the last four digits. Now subtract 79 from this. Should the 34s display zero? Of course it shouldn't. And that's why it shouldn't claim 78,99999999999999 is prime either, or maybe even INT(x) = 79 just because that's what you see in the display.

Dieter

            
Re: WP-34S (Prime Number Test) question
Message #3 Posted by Barry Mead on 20 Oct 2013, 5:04 p.m.,
in response to message #2 by Dieter

That is not exactly true. For instance the Even? and Odd? tests show false for numbers like 78.1 and 79.1, although the underlying integer 78 and 79 are Even and Odd respectively. These tests distinguish between whole numbers and fractional numbers, but the Prime number test does not, and when it converts a non-whole to an integer it does it in a way that is less representative of the displayed number than it could be. In my humble opinion.

                  
Re: WP-34S (Prime Number Test) question
Message #4 Posted by Dieter on 20 Oct 2013, 5:28 p.m.,
in response to message #3 by Barry Mead

Quote:
For instance the Even? and Odd? tests show false for numbers like 78.1 and 79.1, although the underlying integer 78 and 79 are Even and Odd respectively
Yes, that's the way these functions are supposed to work. Take a look at the manual (IOP):
EVEN?    Checks if x is integer and even
ODD?     Checks if x is integer and odd
Thus any non-integer will return "false" here.

Compare this with what the manual states for the PRIME? function:

PRIME?   Checks if the absolute value of the integer part of x is prime.
That's why 78,9999... tests false. The integer part of x is 78, and 78 is not prime.

So PRIME? tests if the integer part of x (!) is prime, while ODD? and EVEN? first test if x is an integer at all, and only if it is, they test if this integer is odd resp. even.

So yes, you are right: not all functions work on the full-precision value of X. Some ignore the sign and fractional part of X, such as the PRIME? function. But they do not round to the next integer either.

Dieter

Edited: 20 Oct 2013, 5:32 p.m.


[ Return to Index | Top of Index ]

Go back to the main exhibit hall