JS implements the sum of numbers divided by 3 or 5 in less than 1000, and js division

Source: Internet
Author: User

JS implements the sum of numbers divided by 3 or 5 in less than 1000, and js division

Today, we can see in the Technical Group that the sum of the numbers 3 or 5 divided by less than 1000 is calculated. The interesting solution is solution 2, which was quite confusing at the beginning. Later I thought about it and I thought it was too confusing.

The first solution is very common, that is, to traverse the number of less than 1000, as long as there is a total of 3 or a total of 5, it will add up, and finally get a total sum.

I am interested in the second solution. Here we use the mathematical arithmetic difference series.

The level of the first item of an equal-difference series with a tolerances is:

The first step of the second method is to use (a1 + an)/3*3, so it becomes 3*(1 + 333) * 333/2, this step adds up all the items except 3.

Similarly, sum (Math. in floor (input-1)/5) * 5, all the items that can be divided into 5 within 1000 can be added up and finally divided into 3 and 5 with repeated numbers, for example, if you divide the number by 15, you can get the sum of the numbers less than 1000 that can be divisible by 3 or 5.

The following section describes how to calculate the sum of numbers divided by 3 or 5 in less than 1000 in JS. I hope this will help you!

Articles you may be interested in:
  • Javascript code for Division
  • Analysis of difference between the ceil and floor functions of JavaScript Division

Related Article

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.