Here's a creative method.
I'm gonna write it in PHP, 'cause I don't know Java, and I don't feel like answering a homework question anyway, plus this isn't how your instructor will want it. Anyway, here goes:
PHP Code:
<?php
$lim=100; //set the upper limit
$i=1;
$comp=array(2);
$primes=array(2);
while($comp[0]<=$lim/2) {
$p = 1;
foreach($comp as $n) { $p = $p*$n; }
if ($p<=$lim) {
$nprimes[$i]=$p;
echo implode(', ',$nprimes).'<br>';
$i++;
for($b=2;$comp[count($comp)-$b]!=$comp[count($comp)-1]&&count($comp)>$b;$b++) {}
if (count($comp)==$b) {
$b=1;
}
$comp[count($comp)]=$comp[count($comp)-$b];
}
else {
for($x=2;$x<=10+($lim/2);$x++) {
if (!in_array($x,$nprimes)) {
$primes[$i]=$x;
$comp[count($comp)-2]=$x;
unset($comp[count($comp)-1]);
break;
}
}
}
echo $comp[count($comp)-1]."\n";
}
echo implode(', ',$primes);
?>
Edit: code is updated. it still doesn't work properly. it's a bit harder than I thought to do it backwards. Interesting, though. I may fix this up soon, but I'm not sure I will.
Bookmarks