bugGNU Octave - Bugs: bug #43041, isprime(5i) returns true

 
 

bug #43041: isprime(5i) returns true

Submitter:  Colin Macdonald <cbm>
Submitted:  Thu 21 Aug 2014 04:23:21 PM UTC
   
 
Category:  Octave Function Severity:  3 - Normal
Priority:  5 - Normal Item Group:  Incorrect Result
Status:  Fixed Assigned to:  None
Originator Name:  Open/Closed:  * Closed
Release:  * dev Operating System:  * GNU/Linux
Fixed Release:  None Planned Release:  None
* Mandatory Fields

Add a New Comment Rich Markup
   

Sun 24 Aug 2014 02:45:38 PM UTC, comment #5: 

It is convenient for many purposes to define a prime number as a non-unit number that divides a factor whenever it divides a product (a unit is a number that divides 1). This is the definition that generalises to any integral domain. Following this definition, both Maxima's and GAP's isprime() and IsPrimeInt() respectively consider negative prime numbers to be prime. Interestingly, Pari/GP, which is actually specialised for number theory, excludes negative prime numbers from its isprime() function.

Since I consider "Matlab compatibility" to allow not erroring out when Matlab errors out, and since Matlab errors out on negative integers, I think we can pick whichever definition works out. I prefer to consider negative primes as primes, but if you feel strongly otherwise, feel free to make the change to exclude them or to further document my implementation.

Jordi Gutiérrez Hermoso <jordigh>
Group Member
Sun 24 Aug 2014 12:27:49 AM UTC, comment #4: 

Is it correct to return


isprime (abs (x))


when x is real and less than 0?  Every web source defines a real-valued prime on the positive integer range [1, Inf].  As an example, http://mathworld.wolfram.com/PrimeNumber.html.

It seems to be more appropriate to issue an error in this case.  Or, if the calculation should proceed then to issue NaN since it is not definitionally a prime number, even if it meets the test for being a prime.

In this case, -5, looks like a duck, quacks like a duck, but is excluded by definition from actually being a duck.



Rik <rik5>
Group administrator
Sat 23 Aug 2014 10:31:37 PM UTC, comment #3: 
Jordi Gutiérrez Hermoso <jordigh>
Group Member
Sat 23 Aug 2014 07:37:08 PM UTC, comment #2: 

The bug is not that isprime(7i) is true, because it is, but that isprime(5i) is true (which it's not, since 5i = (2 + i)(1+2i).

I'll patch this. There's no reason why this function should be restricted to positive integers.

Jordi Gutiérrez Hermoso <jordigh>
Group Member
Thu 21 Aug 2014 11:12:17 PM UTC, comment #1: 

isprime now checks whether the input is a real number (http://hg.savannah.gnu.org/hgweb/octave/rev/0976f9fccbbd).  Closing report.

Rik <rik5>
Group administrator
Thu 21 Aug 2014 04:23:21 PM UTC, original submission:  

isprime(4i) is false which I can live with :)

isprime(7i) gives true: it should either give false or raise an exception.

Matlab gives an error "All entries of X must be nonnegative integers."

---------------------

Our error message for isprime(-1) needs a tweak too:
"error: isprime: needs positive integers".

Should say "nonnegative integers"

---------------------

I might around to patching this but submitting the bug in case I don't!

Colin Macdonald <cbm>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by jordigh (Posted a comment)
  • -email is unavailable- added by rik5 (Posted a comment)
  • -email is unavailable- added by cbm (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only group members can vote.

     

    Follow 7 latest changes.

    Date Changed by Updated Field Previous Value => Replaced by
    2014-08-23 jordigh StatusConfirmed Fixed
        Open/ClosedOpen Closed
    2014-08-23 jordigh Open/ClosedClosed Open
    2014-08-23 jordigh StatusFixed Confirmed
        Summaryisprime(7i) returns true isprime(5i) returns true
    2014-08-21 rik5 StatusNone Fixed
        Open/ClosedOpen Closed

    Back to the top

    Powered by Savane 3.13-758e.
    Corresponding source code