138-Street Numbers // brute force or equation solving

Source: Internet
Author: User

Bs first. You can create a table before submitting this question.

At the beginning, there was a problem with binary writing. I first found n and then m. In fact, we need to first find m and then find n.

Another solution is to solve the equation and solve the Pell Equation.

You must first find the expression for both brute force and equation solving. N * (n + 1)/2 = x * (x + 1)/2-n * (n + 1) + n; n * n/2 = x * x + x. Then it is transformed into a standard Pel equation, and then solved by recursion.

The following is the binary code:

#include<iostream>#include<cstdio>using namespace std;const int n=~(unsigned int)0/2;int main(){    int t=0;    for(long long i=6;i<n&&t<=10;i++)    {        long long min=1;        long long max=i+1;        long long mid;        while(min<=max)        {            mid=(min+max)/2;            if(mid*mid*2==i*(i+1))            {                printf("%10lld%10lld\n",mid,i);                t++;                break;            }            else if(mid*mid*2>i*(i+1)) max=mid-1;            else min=mid+1;        }    }    return 0;}

It takes about 3 minutes to obtain 10 groups of solutions. .....

The code for solving the equation is as follows:

#include<iostream>#include<cstdio>using namespace std;int main(){    int x3,y3;    for(int x1=1,y1=1,x2=1,y2=1,t=0;t<10;t++)    {        x3=(2*x1+1)*(2*x2+1)+8*y1*y2;        y3=(2*x1+1)*y2+y1*(2*x2+1);        printf("%10d%10d\n",y3,(x3-1)/2);        x2=(x3-1)/2;        y2=y3;    }    return 0;}

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.