Trying to make an algorithm with php


#1

Hi there,

I’m a totalt noob with php, and I need some help.

This is what I want to do with php:

while ($x <= 100)

$d = 1;

$r = $x/$d;

If $r is a whole number ie 2, then save $d to file.txt on the first line with ; on the end. And repeat this until $r is not a whole number.

If $r is not a whole number ie 2,3 and $d is not greater than 1/2 of $x, then $d++; and then do the above.

If $d is greater than $x, then $x++ and reset $d to one. And repeat the whole prosess

Heres an example:

Number 20 ($x) is chosen

It is divided by 2 ($d) = 10 ($r)

$r is a whole number therefore we save $d to file.txt
Since 2 is not greater 1/2 of 10, we dividie 10 by 2.

We get 5.

5 is whole number, we therefor save 2 it to file.txt with a ; on the end. The first line of file.txt now looks like 2;2;

since 2 is not greater than 1/2 of 5, we divide 5 by 2. But since that leaves us with a non-whole number 2.5, we don’t save 2 to the file.
We don’t try to divide 5 by 3 since 3 is greater 1/2 of 5. Since $d has become bigger than one half of $r, we save $r to file.txt. Or txt file now looks like:

2;2;5;

Now, we go from 20 to 21.

We divide 21 by 2, but that gives 10,5 which is not a whole number. We therefore try to divide 21 by 3, which leaves us with 7, which is a whole number. We therefor save 3 to the second line of txt file.

We try dividing 7 by 3, but that gives a nonwhole number. But we don’t try dividing 7 by 4 because 4 is greater than 1/2 of 7. We now save 7 to the second line of file.txt

Our file.txt looks like this:

2;2;5;
3;7;

And this procedure continues to a given number.

I hope i was clear. Please help. Thanks

Nadeem