T
T
Torento203452021-09-25 00:07:08
JavaScript
Torento20345, 2021-09-25 00:07:08

Which algorithm for calculating the multiplicity of numbers is more efficient?

There is a task to get all multiples of 3 and 5.
Can it be solved somehow more efficiently?
https://jsfiddle.net/7pa4Luzn/

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Adamos, 2021-09-25
@Torento20345

If you need to determine the multiplicity, then we take the textbook Mathematics, grade 6.
Chapter Signs of multiplicity 3, 5 and 9 with a minimum of calculations.
If you need to fill the array, then the time spent on filling it, and in the jaboscript, is orders of magnitude higher than the cost of this simple check.
It can speed up (and maybe slow down) this process by replacing ++i in the loop with adding the number that really needs to be added to get the next multiple - it cycles: [3, 2, 1, 3, 1, 2, 3]. The check will be removed, but the calculations, in fact, will only increase. But without branching and empty cycles.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question