Multiples of 2 3 5 7

Source: Internet
Author: User

Topic Five, 2 3 5 7multiples of

Given a number n, ask 1 to N, how many are not multiples of 2 3 5 7. For example, n = 10, only 1 is not a multiple of 2 3 5 7.

Input

Enter 1 number n (1 <= n <= 10^18).

Output

The output is not the number of multiples of 2 3 5 7.

Input example

10

10000

1234567890

98765

11111111111111111

Output example

1

2285

282186946

22575

2539682539682540

Problem Solving Ideas:

First say I see the idea of this problem, enumerate all integers 1 through N with an if judgment i%2| | i%3| | i%5| | I%7, if true, then the ans counter plus one, time complexity O (n),

In the case of 1 <= N <=10^18 directly GG Bar.

So I think that in the prime number of the Euler sieve in the table, you can imagine that the 1 to N all the numbers in 10 units, the second fourth fifth sixth eighth tenth number do not judge, only need to judge each group, The first element, the third element, the seventh element, and the nineth element are multiples of 3 and 7 . This time complexity is slightly lower than the first one, encountered the tree or directly GG .

The third method I changed a train of thought, the same remainder theorem, the specific theory Baidu is good. In the subject, the number in multiples of 1 to N is not 2 3 5 7

= number of multiples of 2 in N-n

-N is the number of multiples of 3

-N is the number of multiples of 5

-N is the number of multiples of 7

+n is the number of multiples of 2 and 3 .

+n is the number of multiples of 2 and 5 .

+n is the number of multiples of 2 and 7 .

+n is the number of multiples of 3 and 5 .

+n is the number of multiples of 3 and 7 .

+n is the number of multiples of 5 and 7 .

The number of multiples of 2,3,5 in -n

The number of multiples of 2,3,7 in -n

The number of multiples of 2,5,7 in -n

The number of multiples of 3,5,57 in -n

+n is the number of multiples of 2,3,5,7 ;

The time complexity drops from o (n) to O (1), the code is the addition and subtraction of several division operations to calculate the results, note that the n here to a long Long storage.

Code:

#include <cstdio>intMain () {Long LongN; Long LongCount =0; scanf ("%lld", &N); Count= N-n/2N3N5N7+ N/(2*3) + N/(2*5) + N/(2*7) + N/(3*5) + N/(3*7) + N/(5*7)              -N/(2*3*5)-N/(2*5*7)-N/(2*3*7)-N/(3*5*7)              + N/(2*3*5*7); printf ("%lld", Count); return 0;}

Multiples of 2 3 5 7

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.