Ultraviolet (a) 100-The 3N + 1 problem

Source: Internet
Author: User
Tags cmath

This question is very simple, but its background (the craz question, or angular conjecture) is not that simple. No one has yet provided a reliable proof.

Analysis:

You only need to enumerate and simulate each positive integer in the given range. However, this will definitely time out. We can use a memory-like idea to save the small number of results with an array, and then directly reference it when calculating a large number. However, not all data results can be saved. The experiment shows that the possible number in the calculation process is much larger than the upper limit of the given range. (See the attached table). Therefore, when calculating a large number (I took> = 10,000,000), we need to combine the direct enumeration with the memory search. I actually tried two methods. The first method is to enumerate the numbers greater than or equal to 10,000,000 in 0.068 seconds. The second method is to enumerate and memorize the numbers greater than or equal to 10,000,000, it takes 0.052 s, which is faster.

Note:

1. the number to be counted includes itself and the last generated one.

2. There is no limit on the relative size of the two numbers I and J in the question. That is to say, I> J may occur and need to be judged.

Code:

Method 1:

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<ctime>#include<memory.h>using namespace std;typedef long long LL;int a[10000005];int enumerate(LL x){    int counts=1;    while(x!=1)    {        if(x%2==0) x/=2;        else x=x*3+1;        counts++;    }    return counts;}int search(LL x){    if(x>=10000000) return enumerate(x);    if(a[x]!=-1) return a[x];    if(x%2==0)        return a[x]=search(x/2)+1;    else        return a[x]=search(3*x+1)+1;}int main(){    int m,n,i,ans;    memset(a,-1,sizeof(a));    a[1]=1;    while(cin>>m>>n)    {        cout<<m<<‘ ‘<<n;        ans=-1;        if(m>=n) swap(m,n);        for(i=m;i<=n;i++)            ans=max(ans,search(i));        cout<<‘ ‘<<ans<<endl;    }    return 0;}

 

Method 2:

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<ctime>#include<memory.h>using namespace std;typedef long long LL;int a[10000005];int enumerate(LL x){    int counts=1;    while(x>=10000000)    {        counts++;        x = x%2==0 ? x/2 : x*3+1;    }    if(a[x]!=-1) return a[x];    if(x%2==0)        return a[x]=enumerate(x/2)+counts;    else        return a[x]=enumerate(3*x+1)+counts;}int main(){    int m,n,i,ans;    memset(a,-1,sizeof(a));    a[1]=1;    while(cin>>m>>n)    {        cout<<m<<‘ ‘<<n;        ans=-1;        if(m>=n) swap(m,n);        for(i=m;i<=n;i++)            ans=max(ans,enumerate(i));        cout<<‘ ‘<<ans<<endl;    }    return 0;}

 

Appendix:

Range Maximum value that may be encountered during Calculation Answer
[1, 10] 52 20
[1,100] 9232 119
[1, 1000] 250504 179
[1, 10000] 27114424 262
[1, 100000] 1570824736 351
[1, 1000000] 56991483520 525

 

 

 

 

 

Ultraviolet (a) 100-The 3N + 1 problem

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.