Return to Snippet

Revision: 13470
at April 24, 2009 19:33 by cobra90nj


Updated Code
<?php

define('N', 200);

function isPrime($num) 
{
	if ($num == 2 || $num == 3) { return 1; }
	if (!($num%2) || $num<1)    { return 0; }
	
	for ($n = 3; $n <= $num/2; $n += 2) {
		if (!($num%$n)) {
			return 0;
		}
	}
	
	return 1;
}

for ($i = 2; $i <= N; $i++) {
	if (isPrime($i)) {
		$sum += $i;
	}
}

echo $sum;

Revision: 13469
at April 24, 2009 19:33 by cobra90nj


Updated Code
<?php

define('N', 200);

function isPrime($num) 
{
	if ($num == 2 || $num == 3) { return 1; }
	if (!($num%2) || $num<1) 	{ return 0; }
	
	for ($n = 3; $n <= $num/2; $n += 2) {
		if (!($num%$n)) {
			return 0;
		}
	}
	
	return 1;
}

for ($i = 2; $i <= N; $i++) {
	if (isPrime($i)) {
		$sum += $i;
	}
}

echo $sum;

Revision: 13468
at April 24, 2009 19:25 by cobra90nj


Initial Code
<?php

define('N', 200);

function isPrime($num) 
{
    if ($num<1)    { return 0; }
	if ($num == 2) { return 1; }
	if ($num == 3) { return 1; }
	if (!($num%2)) { return 0; }
	
	for ($n = 3; $n <= $num/2; $n += 2) {
		if (!($num%$n)) {
			return 0;
		}
	}
	
	return 1;
}

for ($i = 2; $i <= N; $i++) {
	if (isPrime($i)) {
		$sum += $i;
	}
}

echo $sum;

Initial URL

                                

Initial Description

                                

Initial Title
Sum the number prime

Initial Tags
php, number

Initial Language
PHP