JS realizes the _javascript technique of numbers divisible by 3 or 5 within 1000

Source: Internet
Author: User
Tags arithmetic

Today, we see a mention of this in the tech group: the sum of numbers that are divisible by 3 or 5 within 1000. It is interesting to see the solution two, the beginning is quite confused, and then thought, and then by people pointing, feel the Enlightenment.

The first solution is very common, that is, 1000 of the number are traversed once, as long as there is division 3 or divisible by 5 of the number, add it up, and finally get a total sum.

What interests me is the second solution, where the mathematical arithmetic progression is cleverly applied.

The arithmetic progression of a tolerance for the preceding paragraph is:

The second method uses the first step (A1+an)/3*3, so it becomes the 3* (1+333) *333/2, which adds up all the items that divide 3.

Similarly, in sum (Math.floor (input-1)/5) *5, add up all items that can divide 5 within 1000, and eventually divide 3 and divide 5 with duplicate numbers, such as 15, and then subtract the repetition by the number 15, You can get the sum of numbers that are divisible by 3 or 5 within 1000.

About the JS implementation of 1000 or 3 or 5 divisible by the number of the sum of the contents of the content will be introduced to everyone, I hope to help!

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.