Core Function IsPrime

From Sputnik Wiki
Jump to: navigation, search
IsPrime( <expression> )

Contents

Description

Check is a given number is a valid Prime number.

Parameters

expression

A number to check.

Return Value

Success: Returns true.

Failure: Returns false.

Remarks

If your variable is an INT type the prime check will use Int64.

If your variable is a Float type the prime check will be as a Double.

Example

Prime the first 1000 primes

my $PrimeCount = 0;
for(my $i = 0; $i < 7920; $i++)
{
	my $Number = (double)$i;
	$IsPrime = IsPrime($Number);
	if($IsPrime)
	{
		println("'$Number' is prime" );
		$PrimeCount++;
	}
}
say "Found '$PrimeCount' prime number(s)";

Check if a list of numbers are primes

my $Primes = qw(
      2      3      5      7     11     13     17     19     23     29 
     31     37     41     43     47     53     59     61     67     71 
     73     79     83     89     97    101    103    107    109    113 
    127    131    137    139    149    151    157    163    167    173 
    179    181    191    193    197    199    211    223    227    229 
    233    239    241    251    257    263    269    271    277    281 
    283    293    307    311    313    317    331    337    347    349 
   );
 
while( $Primes =~ m/(\d+)/g )
{
	my $Number = $_rg[$_][1];
	$IsPrime = IsPrime($Number);
	println("Is '$Number' Prime? $IsPrime" );
}
// Prints:
Is '2' Prime? true
// Is '3' Prime? true
// Is '5' Prime? true
// Is '7' Prime? true
// Is '11' Prime? true
// Is '13' Prime? true
// Is '17' Prime? true
// Is '19' Prime? true
// Is '23' Prime? true
// Is '29' Prime? true
// Is '31' Prime? true
// Is '37' Prime? true
// Is '41' Prime? true
// Is '43' Prime? true
// Is '47' Prime? true
// Is '53' Prime? true
// Is '59' Prime? true
// Is '61' Prime? true
// Is '67' Prime? true
// Is '71' Prime? true
// Is '73' Prime? true
// Is '79' Prime? true
// Is '83' Prime? true
// Is '89' Prime? true
// Is '97' Prime? true
// Is '101' Prime? true
// Is '103' Prime? true
// Is '107' Prime? true
// Is '109' Prime? true
// Is '113' Prime? true
// Is '127' Prime? true
// Is '131' Prime? true
// Is '137' Prime? true
// Is '139' Prime? true
// Is '149' Prime? true
// Is '151' Prime? true
// Is '157' Prime? true
// Is '163' Prime? true
// Is '167' Prime? true
// Is '173' Prime? true
// Is '179' Prime? true
// Is '181' Prime? true
// Is '191' Prime? true
// Is '193' Prime? true
// Is '197' Prime? true
// Is '199' Prime? true
// Is '211' Prime? true
// Is '223' Prime? true
// Is '227' Prime? true
// Is '229' Prime? true
// Is '233' Prime? true
// Is '239' Prime? true
// Is '241' Prime? true
// Is '251' Prime? true
// Is '257' Prime? true
// Is '263' Prime? true
// Is '269' Prime? true
// Is '271' Prime? true
// Is '277' Prime? true
// Is '281' Prime? true
// Is '283' Prime? true
// Is '293' Prime? true
// Is '307' Prime? true
// Is '311' Prime? true
// Is '313' Prime? true
// Is '317' Prime? true
// Is '331' Prime? true
// Is '337' Prime? true
// Is '347' Prime? true
// Is '349' Prime? true
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox