solutions/cw3/factors.while
author Christian Urban <christian.urban@kcl.ac.uk>
Fri, 04 Nov 2022 12:07:40 +0000
changeset 894 02ef5c3abc51
parent 864 solution/cw3/factors.while@b5b1bc0a603b
child 905 15973df32613
permissions -rw-r--r--
updatedHG: added solutions/cw5/fun_tokens.sc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
864
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     1
// Find all factors of a given input number
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     2
// by J.R. Cordy August 2005
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     3
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     4
write "Input n please";
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     5
read n;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     6
write "The factors of n are:\n";
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     7
f := 2;
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     8
while n != 1 do {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
     9
    while (n / f) * f == n do {
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    10
        write f; write "\n";
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    11
        n := n / f
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    12
    };
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    13
    f := f + 1
Christian Urban <christian.urban@kcl.ac.uk>
parents:
diff changeset
    14
}