Bored, time for a small programming game. Here it is, plain and simple. I tell you a function you have to write (and what must be done in that function), and whoever can code it in the smallest amount of characters wins. It will teach you to optimize your code, although you might have to use some abhorred programming techniques along the way.
You can use any language.
Not limited to one entry per person. If someone beats you, go back to your code and try and make it smaller. Once nobody can beat it anymore, game over.
Prize: 50k LG to winner.
Function you have to make
Return a list of all prime numbers between a lower limit and upper limit.
Function must:
- be named Primes.
- not return 1 as a prime number (i.e. if 1 is entered as lower limit, 2 will be the first prime).
- return null result if upper limit lower than lower limit.
Rules:
- spaces not included in total character length (new lines don't count as well)
- don't just use someone elses code and optimize it. Use your own code.
My entry (coded in PHP)
Char length: 201.PHP Code:
function Primes($l, $u)
{
if($l <= 1) $l = 2;
if($u <= $l) return;
for($x = $l; $x <= $u; $x++)
$z[] = $x;
for($i = 0; $c*$c < $u; $i++)
{
$c = $z[$i];
foreach($z as $d => $v)
if($v % $c == 0 && $v != $c)
unset($z[$d]);
$z = array_values($z);
}
return $z;
}
Code:print_r(Primes(0,30)); returns:
Array
(
[0] => 2
[1] => 3
[2] => 5
[3] => 7
[4] => 11
[5] => 13
[6] => 17
[7] => 19
[8] => 23
[9] => 29
)