something seems wrong here

28 views
Skip to first unread message

Michael Beeson

unread,
Jan 1, 2022, 7:00:56 PM1/1/22
to sage-support

sage: d = 6

sage: p = d/2

sage: p

3

sage: is_prime(p)

False      #  Huh?!!  

sage: is_prime(3)

True

sage: p==3

True

This happens in version 8.7  and also in the current version (installed yesterday)


William Stein

unread,
Jan 1, 2022, 7:28:38 PM1/1/22
to sage-support
On Sat, Jan 1, 2022 at 4:01 PM Michael Beeson <profb...@gmail.com> wrote:
>
> sage: d = 6
>
> sage: p = d/2
>
> sage: p
>
> 3
>
> sage: is_prime(p)
>
> False # Huh?!!

The parent of p is QQ (the rational field).
p is not a prime number of the **rational field**.
You should coerce p to ZZ first.

In retrospect, we probably should have just made is_prime throw an
exception if the parent ring is a field, since the ring theory
definition of prime number isn't very useful in a field.

>
> sage: is_prime(3)
>
> True
>
> sage: p==3
>
> True
>
> This happens in version 8.7 and also in the current version (installed yesterday)
>
>
> --
> You received this message because you are subscribed to the Google Groups "sage-support" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sage-support...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sage-support/9cb13872-298c-4463-b27d-b77ea288bfedn%40googlegroups.com.



--
William (http://wstein.org)

William Stein

unread,
Jan 1, 2022, 7:30:30 PM1/1/22
to sage-support

slelievre

unread,
Jan 2, 2022, 4:46:36 AM1/2/22
to sage-support
2022-01-02 00:00:56 UTC, Michael Beeson on sage-devel:


> sage: d = 6
> sage: p = d/2
> sage: p
> 3
> sage: is_prime(p)
> False      #  Huh?!!  
> sage: is_prime(3)
> True
> sage: p==3
> True
>
> This happens in version 8.7  and also in the current version (installed yesterday)

William Stein answered:


> The parent of p is QQ (the rational field).
> p is not a prime number of the **rational field**.
> You should coerce p to ZZ first.

To change `p` to an integer before testing whether it is prime:

```
sage: d = 6
sage: parent(d)
Integer Ring

sage: p = d/2
sage: parent(p)
Rational Field

sage: p = ZZ(p)
sage: is_prime(p)
True
```

Or, to take quotients in the integers from the start, use

```
sage: d = 6
sage: p = d // 2
sage: parent(p)
Integer Ring
sage: is_prime(p)
True
```

The behaviour of `is_prime` for field elements is the object of

- Sage Trac ticket #17919
  Disallow is_prime() for FieldElement
  https://trac.sagemath.org/ticket/17919

- Sage Trac ticket #32321
  Make is_prime() return true for rational numbers that are prime as integers
  https://trac.sagemath.org/ticket/32321

See also:

- Sage Trac ticket #32340
  Document behavior of .is_prime() for number fields
  https://trac.sagemath.org/ticket/32340

- Sage Trac tickets whose summary contains is_prime
  https://trac.sagemath.org/query?order=id&desc=1&summary=~is_prime

Reply all
Reply to author
Forward
0 new messages