How to resolve the algorithm Sum multiples of 3 and 5 step by step in the PHP programming language
How to resolve the algorithm Sum multiples of 3 and 5 step by step in the PHP programming language
Table of Contents
Problem Statement
The objective is to write a function that finds the sum of all positive multiples of 3 or 5 below n. Show output for n = 1000. This is is the same as Project Euler problem 1. Extra credit: do this efficiently for n = 1e20 or higher.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Sum multiples of 3 and 5 step by step in the PHP programming language
The provided PHP code is designed to calculate the sum of all multiples of 3 or 5 below a specified maximum value. It utilizes three different approaches for this calculation. Here's a detailed explanation of each approach:
1. Using a Simple Loop:
In the first code block, a straightforward loop is employed to iterate through numbers from 1 to the specified maximum value (excluding the maximum itself). For each number, it checks if it's divisible by 3 or 5 (i.e., if the remainder is 0 when divided by 3 or 5). If the condition is met, the number is added to a running sum. Finally, the total sum is printed to the console.
2. Using Helper Functions:
In the second code block, two helper functions are defined:
sum_multiples($max, $divisor)
: This function calculates the sum of all multiples of a given divisor ($divisor
) up to a specified maximum value ($max
). It uses a mathematical formula to compute the sum based on the number of multiples and the divisor.sum_multiples($max, $divisor)
: This function calculates the sum of all multiples of a given divisor ($divisor
) up to a specified maximum value ($max
). It uses a mathematical formula to compute the sum based on the number of multiples and the divisor.
The main code then calculates the sum of multiples for divisors 3 and 5 up to the specified maximum and subtracts the sum of multiples for divisor 15 (which overlaps with both 3 and 5). The resulting sum is printed to the console.
3. Using GMP Extension for Large Numbers:
In the third code block, the popular GMP (GNU Multiple Precision) extension is utilized to handle large numbers efficiently. The sum_multiples_gmp($max, $divisor)
function is similar to the sum_multiples
function, but it uses GMP functions for arithmetic operations on large integers.
The main code iterates through a range of maximum values and calculates the sum of multiples for divisors 3 and 5 up to each maximum. It then subtracts the sum of multiples for divisor 15. The results are printed to the console, showcasing the performance of the GMP extension in handling large numbers.
Source code in the php programming language
$max = 1000;
$sum = 0;
for ($i = 1 ; $i < $max ; $i++) {
if (($i % 3 == 0) or ($i % 5 == 0)) {
$sum += $i;
}
}
echo $sum, PHP_EOL;
function sum_multiples($max, $divisor) {
// Number of multiples of $divisor <= $max
$num = floor($max / $divisor);
// Sum of multiples of $divisor
return ($divisor * $num * ($num + 1) / 2);
}
$max = 1000;
$sum = sum_multiples($max - 1, 3)
+ sum_multiples($max - 1, 5)
- sum_multiples($max - 1, 15);
echo $sum, PHP_EOL;
function sum_multiples_gmp($max, $divisor) {
// Number of multiples of $divisor <= $max
$num = gmp_div($max, $divisor);
// Sum of multiples of $divisor
return gmp_div(gmp_mul(gmp_mul($divisor, $num), gmp_add($num, 1)), 2);
}
for ($i = 0, $n = gmp_init(10) ; $i < 21 ; $i++, $n = gmp_mul($n, 10)) {
$max = gmp_sub($n, 1);
$sum =
gmp_sub(
gmp_add(
sum_multiples_gmp($max, 3),
sum_multiples_gmp($max, 5)
),
sum_multiples_gmp($max, 15)
);
printf('%22s : %s' . PHP_EOL, gmp_strval($n), $sum);
}
You may also check:How to resolve the algorithm Monte Carlo methods step by step in the AWK programming language
You may also check:How to resolve the algorithm Determine if a string is squeezable step by step in the Go programming language
You may also check:How to resolve the algorithm Random numbers step by step in the Octave programming language
You may also check:How to resolve the algorithm Reflection/List properties step by step in the Ruby programming language
You may also check:How to resolve the algorithm Polynomial long division step by step in the Tcl programming language