Home > Backend Development > PHP Tutorial > The sum of values ​​that are multiples of N in the interval, algorithm

The sum of values ​​that are multiples of N in the interval, algorithm

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-08-10 09:07:34
Original
1505 people have browsed it

Example: Find the sum of values ​​between 10-50 that are multiples of 7. There are special rewards
I often encounter some problems when learning PHP. What about daffodils, what are the sums of prime numbers, etc. Today I I saw this question on a website. The answer to this question is basically the following function.

<code>function newSumMultiple($min,$max,$multiple){
    $sum = 0;
    for(;$min<$max;++$min){
        if(!($min % $multiple)){
            $sum += $min;
        }
    }
    return $sum;
}</code>
Copy after login
Copy after login

I had a sudden thought today. It has always been like this. When the teacher taught, he always followed the instructions and I was really speechless. So I thought of the following idea.

<code>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;
}</code>
Copy after login
Copy after login

Asking for a new algorithm, it is best to attach the code.

Reply content:

Example: Find the sum of values ​​between 10-50 that are multiples of 7. There are special rewards
I often encounter some problems when learning PHP. What about daffodils, what are the sums of prime numbers, etc. Today I I saw this question on a website. The answer to this question is basically the following function.

<code>function newSumMultiple($min,$max,$multiple){
    $sum = 0;
    for(;$min<$max;++$min){
        if(!($min % $multiple)){
            $sum += $min;
        }
    }
    return $sum;
}</code>
Copy after login
Copy after login

I had a sudden thought today. It has always been like this. When the teacher taught, he always followed the instructions and I was really speechless. So I thought of the following idea.

<code>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;
}</code>
Copy after login
Copy after login

Asking for a new algorithm, it is best to attach the code.

Use min and max to divide multiple to get the upper and lower bounds respectively, and then use the arithmetic sequence summation formula

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template