For example, the sum of the values between 10 and 50 is a multiple of 7. if you have special prizes, you will always encounter some questions when learning PHP. what are the daffodils? what are the prime numbers. today I see this question on a website. The answer to this question is basically the following function. {code ...} today I burst into a storm... for example, the sum of the values between 10 and 50 is a multiple of 7. special rewards
I often encounter some questions when I am learning PHP. what are the daffodils? what are the prime numbers. today I see this question on a website. The answer to this question is basically the following function.
function newSumMultiple($min,$max,$multiple){ $sum = 0; for(;$min<$max;++$min){ if(!($min % $multiple)){ $sum += $min; } } return $sum;}
Today, I have been thinking like this all the time. when my teacher taught me, I learned the following things.
function sumMultiple($min,$max,$multiple){ $remainder=$min % $multiple; if($remainder){ $remainder = $min + $remainder + 1; $min = $remainder; }else{ $remainder = $min; } $sum = 0; while(true){ $sum += $remainder; $min += $multiple; if($min >= $max){ break; } $remainder = $remainder + $multiple; } return $sum;}
For a new algorithm, it is best to attach the code.
Reply content:
For example, the sum of values between 10 and 50 is a multiple of 7. special prizes are given.
I often encounter some questions when I am learning PHP. what are the daffodils? what are the prime numbers. today I see this question on a website. The answer to this question is basically the following function.
function newSumMultiple($min,$max,$multiple){ $sum = 0; for(;$min<$max;++$min){ if(!($min % $multiple)){ $sum += $min; } } return $sum;}
Today, I have been thinking like this all the time. when my teacher taught me, I learned the following things.
function sumMultiple($min,$max,$multiple){ $remainder=$min % $multiple; if($remainder){ $remainder = $min + $remainder + 1; $min = $remainder; }else{ $remainder = $min; } $sum = 0; while(true){ $sum += $remainder; $min += $multiple; if($min >= $max){ break; } $remainder = $remainder + $multiple; } return $sum;}
For a new algorithm, it is best to attach the code.
Use min and max to separate the multiple values to obtain the upper and lower bounds, and then use the sum formula of the arithmetic difference series.