• 15Feb

    I’ve decided to record how to make complete solutions for a certain problem from a High School programming competition, and the solution for it.

    The problem: You are to write a program which will input a positive integer n and output the number of
    divisors of n and the sum of those divisors. (Note: by divisor we mean a positive integer
    divisor this includes 1 and n.)

    Source code after the jump

    <?php
    //Problem 01
    echo "Please give me a positive integer: ";
    $n = (int)trim(fgets(STDIN));
    $d = 0;
    $s = 0;
    for($x = 1; $x <= $n; $x++){
        if($n%$x == 0){
            $d++;
            $s += $x;
        }
    }
    echo "$n has $d divisors and their sum is $s.\n";
    
    ?>
    

    Posted by Kloplop321 @ 9:55 am

    Tags: , , , , , ,

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.