This was one of the first programs I made and it was a number of years ago. My style was very poor
here's the relevant code. primefind gets called first with the first argument being the number in question, startnum is initially 1, and handel is unrelated to prime factoring:
[PHP]
<?php
$num = 15989;
$startnum = 1;
if($num == 1){
print("<link rel=\"stylesheet\" type=\"text/css\" href=\"external.css\" />The only factor of 1 is 1.<br /> However 1 is not prime because there are no two numbers which can be multiplied together to make 1.<br /> Therefore 1 does not fit the definition of a prime number.");
exit;
}
if($num == 0){
print("<link rel=\"stylesheet\" type=\"text/css\" href=\"external.css\" />The only factor of 0 is 0.<br /> However 0 is not prime because there are no two numbers which can be multiplied together to make 0.<br /> Therefore 0 does not fit the definition of a prime number.");
exit;
}else{
print("The factors of ".$num." are:<br />");
}
primefind($num, $startnum);//calls this functin
if($num == 0){
print("0");
}
else if($num == 1){
print("1");
}else{
}
function primefind($num, $startnum){
$primestat = 'f';
for($counter1 = $startnum; $counter1<=$num; $counter1++){
for($counter2 = 2; $counter2<$counter1; $counter2++){
$primecheck = $counter1%$counter2;
if($primecheck != 0){
$primestat = 't';
}else{
$primestat = 'f';
break;
}
}
if($primestat == 't'||$counter1 == 2){
factorcheck($counter1, $num);
break;
}
}
}
function factorcheck($prime, $num){
$remainder = $num%$prime;
if($remainder == 0)
{
print($prime.'<br />');
$startnum = 1;
primefind(($num/$prime), $startnum);
//exit;
return $prime;
}
else{
$prime++;
primefind($num, $prime);
}
}
?>
[/PHP]
I began writing pseudo code but I got stuck and the following is incomplete. I don't know how to account for when one function calls another in pseudo code.
Terms & Conditions:
0.1 let num be an integer greater than 2
0.2 % denotes the modulus operator
START
1. set counter1 = 1
2. while(counter1 is less than or equal to num) do
2.1 set counter2 = 2[/FONT]
2.2 while(counter 2 is less than counter1) do
2.2.1 set primecheck = counter1%counter2
2.2.2 if(primecheck is not equal to 0) then
2.2.2 set primestat = ‘t’
2.2.3 else
2.2.3 set primestat = ‘f’
2.2.3 goto END
2.2.3 end if
2.2.4 set counter2 = counter2+1
2.3 if(primestat equals ‘t’ OR counter1 equals 2) then
2.3.1 set prime=counter1
2.3.2 set remainder=prime%num
2.3.3 if(remainder is 0) then
2.3.4 set startnum=1
2.3.5 else
2.3.5 set prime=prime+1
2.3.5 goto 1 with new num and startnum=prime
This problem only existed under EasyPHP 1.8 so I'll try to recreate the conditions. After writing this program it became clear to me that primality testing and integer factorizing where two distinct ideas.