Sum of values multiple of N in the interval, algorithm

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.