Calculate the number of 1,500th divisible by 2, 3, or 5.

Source: Internet
Author: User

Method 1: brute-force enumeration 2, 3, 4..., Judge in turn whether N can be divided by 2 | 3 | 5 until 1,500th

#include <iostream>//#include <string.h>using namespace std;int main(int argc, char *argv[]){    int count = 1;    int i;    for(i=2;count!=1500;i++)    {            cout<<i<<endl;            if(i%2==1 || i%3==1 || i%5==1)                      count++;                }    cout<<i<<endl;    getchar();    return 0;}

Let's talk a little bit about the skills. The running speed is similar.

If X can be divisible by 2, 3, or 5, then X must be in the form of 2y, 3Y, or 5y.

Therefore, we only need to list the numbers of 2y, 3Y, and 5y in ascending order until we can list them into 1500, even if we want to solve them.

Here, there may be two or three expressions satisfying at the same time.

#include<iostream>#include<stack>using namespace std;int max(int a,int b,int c){    if(a > b)    {         if(a > c)         {              return a;         }else{               return c;         }    }else    {         if(b>c)         {                return b;         }else{               return c;         }     }}int function(int a,int b,int c,int n){    int count = 1;    int maxa = a;    int maxb = b;    int maxc = c;     while(count<n)    {         cout<<count<<maxa<<maxb<<maxc<<endl;              if(maxa < maxb && maxa <maxc)              {                     count++;                     maxa += a;               }                 else if(maxb < maxa && maxb <maxc)              {                     count++;                     maxb += b;               }              else if(maxc < maxb && maxc <maxa)              {                     count++;                     maxc += c;               }else if(maxa == maxb && maxc > maxa)              {                    count++;                    maxa += a;                    maxb += b;              }              else if(maxc == maxb && maxa > maxc)              {                    count++;                    maxc += c;                    maxb += b;              }              else if(maxa == maxc && maxb > maxa)              {                    count++;                    maxa += a;                    maxc += c;              }else{                    count++;                    maxa += a;                    maxb += b;                    maxc += c;              }                      }        return max(maxa,maxb,maxc);}int main(){    cout<<function(2,3,5,1500);    getchar();    getchar();    return 0;}

I remember another way: the numbers that meet the conditions within 30 are: {2, 3, 4, 5, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20, 21, 22, 24, 25, 26, 27, 28, 30} total 22

So find the number of n that meets the condition, first obtain the number (N/22) * 22 (n is an integer), is (N/22) * 30, then, start from this number and calculate n % 22 numbers that meet the conditions.

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.