No BSD License  

Highlights from
ISPR - Use Java to determine arbitrarily large probable primes

5.0

5.0 | 3 ratings Rate this file 3 Downloads (last 30 days) File Size: 2.31 KB File ID: #5888

ISPR - Use Java to determine arbitrarily large probable primes

by Michael Kleder

 

17 Sep 2004 (Updated 21 Sep 2004)

Use a Java to rapidly determine whether an arbitrarily large positive integer is a prime

Editor's Notes:

This file was a File Exchange Pick of the Week

| Watch this File

File Information
Description

ISPR - Rapidly determine whether an
arbitrarily large positive integer
is a probable prime.

USAGE: q = ispr(n)
       q = ispn(n,1)

n = positive integer, either as a numeric or string datatype

q = 1 if n is prime, 0 if n is composite

1 = any second input will cause the
function to output a message describing
the result in plain language, including
(if n is probably prime) a statement
about the certainty with which n is
claimed to be prime.

Notes: Probable primes are also known as "industrial strength" primes because
of the exceedlingly high probability --
but not certainty -- of primality. This
function utilizes the Java class
"BigInteger" with its method
"isProbablePrime."

For small integers, you can use numeric
inputs; however, for abitrarily large
integers, you must input the number
as a string in order to avoid an
overflow. Note the overflow error in
the second to last example below.

Examples:

>>ispr(314159,1)
I believe that 314159 is prime, but
there is a 1 in 590295810358705650000
chance that I am mistaken.

>>ispr('338327950288419716939',1)
I believe that 338327950288419716939 is
prime, but there is a 1 in 664613997892457940000000000000000000
chance that I am mistaken.

>>ispr(338327950288419716939,1)
338327950288419680000 is definitely not
prime.

>> for i=1:1000;if ispr(i);fprintf('%i ',i);end;end;fprintf('\n')

Michael Kleder, September 2004

MATLAB release MATLAB 7 (R14)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (3)
06 Feb 2006 Gil Roitto  
21 Jul 2008 Claudia Patricia Renteria Mejia

I need this function for develop a cryptography project. I know it will be helpful.

19 Sep 2008 Nikola Toljic

Great. Thanks.

Please login to add a comment or rating.
Updates
21 Sep 2004

Minor bug fix. Now works correctly for primes > 350 digits.

Tag Activity for this File
Tag Applied By Date/Time
symbolic math Michael Kleder 22 Oct 2008 07:31:43
probable Michael Kleder 22 Oct 2008 07:31:43
prime Michael Kleder 22 Oct 2008 07:31:43
large Michael Kleder 22 Oct 2008 07:31:43
integer Michael Kleder 22 Oct 2008 07:31:43
variable Michael Kleder 22 Oct 2008 07:31:43
precision Michael Kleder 22 Oct 2008 07:31:43
java Michael Kleder 22 Oct 2008 07:31:43
biginteger Michael Kleder 22 Oct 2008 07:31:43

Contact us at files@mathworks.com