isPrime in Python

66 views
Skip to first unread message

Jody Crothers

unread,
Oct 14, 2024, 11:15:30 PM10/14/24
to TI-Innovator
Hi All,
I can use the command isPrime in TI Basic
But Im having trouble using it in python.
Does it work in Python?


Jody Crothers


Head of Learning Area (Mathematics)

Ridge View Secondary College

Western Australia

www.jodstar.com

Stephen Arnold

unread,
Oct 14, 2024, 11:43:07 PM10/14/24
to TI-Innovator for Education
(Not sure if previous reply got through - it has been a while!)

Hey Jody

Greetings from the east coast, land of retirement!

Sadly, I haven’t had the chance to get into Python as yet, so cannot answer your question directly.

However, in my free time (just rubbing it in!) I did discover something called Willian’s formula a little while back, which is pretty much the basis for the isPrime function:

isPrime(n) = floor(cos(pi*(((n-1)!)+1)/(n))^2)

(1 if prime, 0 otherwise).

Enjoy!

With best wishes,

Steve

_________________

Steve Arnold

Phone: +(61)4 0175 3834

http://compasstech.com.au

_________________    




Chhaya, Harshal S.

unread,
Oct 15, 2024, 11:16:45 AM10/15/24
to ti-inn...@googlegroups.com

 

I love that talking about primes gets Steve to participate 😊

I now know Steve’s bat-signal 😊

Hi, Steve!

 

(btw, I recently learned about “emirp” numbers and love all the love for primes 😊)

 

To the original question:

Jody – Python doesn’t have a built-in isPrime() function. Yeah, I know…. 😐

 

But thanks to Steve’s fun-looking formula, we have one now.

 

Here’s the code and I have attached the TNS

 

 

from math import *

 

def factorial(n):

  if n == 1:

    return 1

  else:

    return n*factorial(n-1)

   

def isPrime(n):

  retval = floor(cos(pi*((factorial(n-1))+1)/(n))**2)

  return retval;

 

 

for i in range(2, 10):

  print(i, "Prime:?", isPrime(i))

 

 

Hope this helps,

- Harshal

 

 

 

From: ti-inn...@googlegroups.com <ti-inn...@googlegroups.com> On Behalf Of Stephen Arnold
Sent: Monday, October 14, 2024 10:43 PM
To: TI-Innovator for Education <ti-inn...@googlegroups.com>
Subject: [EXTERNAL] Re: isPrime in Python

 

(Not sure if previous reply got through - it has been a while!) Hey Jody Greetings from the east coast, land of retirement! Sadly, I haven’t had the chance to get into Python as yet, so cannot answer your question directly. However, in my free

ZjQcmQRYFpfptBannerStart

This message was sent from outside of Texas Instruments.

Do not click links or open attachments unless you recognize the source of this email and know the content is safe.

    Report Suspicious    ‌

ZjQcmQRYFpfptBannerEnd

--
You received this message because you are subscribed to the Google Groups "TI-Innovator for Education" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ti-innovator...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ti-innovator/e4290039-bf9c-486c-bff3-39329ad6029fn%40googlegroups.com.

isPrime.tns
Message has been deleted
Message has been deleted

Stephen Arnold

unread,
Oct 15, 2024, 6:18:16 PM10/15/24
to ti-inn...@googlegroups.com
Thanks Harshal - still get great pleasure from “lurking” on our TI/T3 groups and following with interest the great work that goes on.

Just one more note on primes(!)

If you want to turbo-charge your prime formula, isPrime(n), just include the Sum of Willan(i) for i from 0 to n.

Since Willan returns 1 for each prime and 0 otherwise, then this will give you which prime n is (or the last prime prior to n if n is composite)!

For example, 

Sum(willan(i) from 0 to 1117) = 187
1117 is the 187th prime,

And

Sum(willan(i) from 0 to 1000) = 168
997 is the 168th prime (the last before 1000!)

isPrime.tns
image0.png

Stephen Arnold

unread,
Oct 16, 2024, 7:18:49 PM10/16/24
to TI-Innovator for Education
Well, after some further digging I find that there is a problem with Willan - the formula stops outputting any value but 0 after n = 13.

However, there is nothing wrong with the formula, or the way it is implemented in Harshal’s Python code.

The problem lies in the math engine’s handling of enormous factorial values.

17! =3.557×10¹⁴ = 355687428096000
By the time we get to 47! we have 59 decimal places!
47!=2.586×10⁵⁹
47! = 2.5862324151116818e+59

Unless you are working in something like Mathematica, this lies beyond the capabilities of any but the most powerful CAS engines - because then we immediately want to ADD 1 to the factorial value, before even proceeding to divide by n, multiply by PI and take the cosine.

While Willan’s formula is interesting and powerful, it is pretty much useless in a practical calculation sense - you would probably be better off running through all potential factors less than n and using this method to decide if a number is prime or not!

With best wishes,

Steve

_________________

Steve Arnold

Phone: +(61)4 0175 3834

http://compasstech.com.au

_________________    


Bert Wikkerink

unread,
Oct 17, 2024, 5:39:08 AM10/17/24
to ti-inn...@googlegroups.com
Perhaps this a solution?

def isPrime(n):

  if n<2:

    return False

  a=int(n**0.5+1)

  for i in range(2,a):

    if n%i == 0:

      return False

  return True

 

n=10756073

 

print(isPrime(n))


Best Wishes,

Bert



-- 

You received this message because you are subscribed to the Google Groups "TI-Innovator for Education" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ti-innovator...@googlegroups.com.

John Hanna

unread,
Oct 17, 2024, 5:46:54 AM10/17/24
to ti-inn...@googlegroups.com

That’s my preference:

Hubert Langlotz

unread,
Oct 17, 2024, 5:56:24 AM10/17/24
to ti-inn...@googlegroups.com, Sebastian Rauh
Yes, this is also a way to find if a number is prime or Not. But here occurs the same problem with complexity. If n is too large you will also get an overflow.
Hubert
Von meinem iPhone gesendet

Am 17.10.2024 um 11:46 schrieb John Hanna <johne...@gmail.com>:



Bert Wikkerink

unread,
Oct 17, 2024, 6:27:39 AM10/17/24
to ti-inn...@googlegroups.com, Sebastian Rauh
Another solution is this:

In a tns document define  a function is_prime(n):=when(isPrime(n),1,0) in a calculator page.
Then in the same document the Python program below uses the built in isPrime() function.

from ti_system import *
n = 10756073
t = eval_function("is_prime",n)
print(t)

Attached is the TNS-file

Best wishes,
Bert

isPrime.tns

Jody Crothers

unread,
Oct 17, 2024, 8:20:33 AM10/17/24
to ti-inn...@googlegroups.com
That will be powerful,
Does it work with all the functions nor in Python?


Jody Crothers


Head of Learning Area (Mathematics)

Ridge View Secondary College

Western Australia

www.jodstar.com


From: ti-inn...@googlegroups.com <ti-inn...@googlegroups.com> on behalf of Bert Wikkerink <b.wik...@gmail.com>
Sent: Thursday, 17 October 2024 6:27 PM
To: ti-inn...@googlegroups.com <ti-inn...@googlegroups.com>
Cc: Sebastian Rauh <s-r...@gmx.de>

Subject: Re: isPrime in Python
--
You received this message because you are subscribed to the Google Groups "TI-Innovator for Education" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ti-innovator...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "TI-Innovator for Education" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ti-innovator...@googlegroups.com.

Hubert Langlotz

unread,
Oct 18, 2024, 3:36:01 AM10/18/24
to ti-inn...@googlegroups.com, Sebastian Rauh
Hi Bert,

this is a fine idea to connect calculator to python, will it work with other functions too.

Sure there is the well known limit for the complexity of the numbers (see below), but it works fine.

Hubert Langlotz



--
You received this message because you are subscribed to the Google Groups "TI-Innovator for Education" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ti-innovator...@googlegroups.com.
Op 17 okt 2024, om 11:56 heeft Hubert Langlotz <langlotz...@t-online.de> het volgende geschreven:


--
You received this message because you are subscribed to the Google Groups "TI-Innovator for Education" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ti-innovator...@googlegroups.com.

Hubert Langlotz

unread,
Oct 18, 2024, 3:39:48 AM10/18/24
to ti-inn...@googlegroups.com
Here is another solution by my colleague Sebastian Rauh

Hubert Langlotz



Anfang der weitergeleiteten Nachricht:

Von: Sebastian Rauh <s-r...@gmx.de>
Betreff: Aw: isPrime in Python
Datum: 17. Oktober 2024 um 12:20:52 MESZ
An: Hubert Langlotz <langlotz...@t-online.de>

Hello @all,

there is another way, which is faster. I exported the candidate to a variable in notes, there the calculation with is_prime is done, and reimported the result.

I attched a file. In comparison my way is a litte faster, but maybe the construction is not as sturdy as the construction in pure python.

Best regards

Sebastian
is_prime.tns

Bert Wikkerink

unread,
Oct 18, 2024, 4:21:55 AM10/18/24
to ti-inn...@googlegroups.com
It works with a functions that needs one numerical argument and returns one numerical value.
But you can use more arguments by first store them.
As an example:

Python does not know the gcd function but TI-Nspire does.
So you can define a function (in a calculator page or a notes page) like gcd1(x):=gcd(a,b) where x is a dummy variable.
Then the Python program can be as follows:

from ti_system import *

def gcd(a,b):
  store_value("a",a)
  store_value("b",b)
  return eval_function("gcd1",0)

print(gcd(132,324))

This stores the values a and b and calculates the gcd(a,b)
(The number 0 is a dummy argument)

Atteched the tns file

Bert

gcd.tns

karen...@gmail.com

unread,
Oct 18, 2024, 9:20:32 PM10/18/24
to TI-Innovator for Education
Hello Steve!! Hope all is well with you.
Karen Campe

Chhaya, Harshal S.

unread,
Oct 24, 2024, 5:08:02 PM10/24/24
to ti-inn...@googlegroups.com

 

I really love how this discussion moved to effective ways of using TI-Nspire CX II math engine and it’s capabilities to extend TI-Nspire CX II’s Python functionality. :)

 

Thanks Bert for the two examples!

 

And we also learned about Willan’s formula and it’s limitations.

 

It’s a prime example (oof! :) of the depth and breadth of knowledge of this amazing community of educators.

 

Thanks and regards,

- Harshal

 

 

 

From: ti-inn...@googlegroups.com <ti-inn...@googlegroups.com> On Behalf Of Bert Wikkerink

Sent: Friday, October 18, 2024 3:22 AM
To: ti-inn...@googlegroups.com
Subject: [EXTERNAL] Re: isPrime in Python

 

It works with a functions that needs one numerical argument and returns one numerical value. But you can use more arguments by first store them. As an example: Python does not know the gcd function but TI-Nspire does. So you can define a function

ZjQcmQRYFpfptBannerStart

This message was sent from outside of Texas Instruments.

Do not click links or open attachments unless you recognize the source of this email and know the content is safe.

 

ZjQcmQRYFpfptBannerEnd

It works with a functions that needs one numerical argument and returns one numerical value.

--

You received this message because you are subscribed to the Google Groups "TI-Innovator for Education" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ti-innovator...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "TI-Innovator for Education" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ti-innovator...@googlegroups.com.

kh7....@gmail.com

unread,
Oct 28, 2024, 1:25:22 PM10/28/24
to TI-Innovator for Education
After reading about the largest prime discovered recently, the 52nd Mersenne Prime, I was wondering if we need to test it with any one of our formulas. Although its discoverer, Luke Durant, appears to have used an innovative approach to prime number discovery
Durant's groundbreaking technique used thousands of GPUs across 24 data centers in 17 countries. Might rule out testing it with the TI-Nspire :-)

The number is 2^136,279,841 - 1, a number with 41,024,320 decimal digits. 

Karlheinz

kh7....@gmail.com

unread,
Oct 28, 2024, 1:29:43 PM10/28/24
to TI-Innovator for Education
By the way, Harshal, there's got to be a tweet somewhere about this that you can use in  "Six STEM Tweets #49."

Karlheinz
Reply all
Reply to author
Forward
0 new messages