Problem G: 深入淺出學算法008-求佩爾方程的解


Description

求關於x y的二次不定方程的解 x2-ny2=1

Input

多組輸入數據,先輸入組數T 然后輸入正整數n(n<=100)

Output

對於每組數據輸出一行,求y<=10000的最小正整數解 ,輸出y的值,如果在此范圍內沒有解則輸出No

Sample Input

1
73

Sample Output

No
#include <stdio.h>
#include <math.h>
int main()
{
    int t,n;
    int i,j;
    int a,b;
    
    while(scanf("%d",&t)!=EOF)
    {
        while(t--)
        { 
            int flag=0;
            scanf("%d",&n);
            for(i=1;i<=10000;i++)
            {
                if((int)sqrt(i*i*n+1)*(int)sqrt(i*i*n+1)==n*i*i+1)
                {
                    printf("%d\n",i);
                    flag=1;
                    break;
                }
            }
            if(flag==0)
            {
                printf("No\n");
            }
        }
    }
    return 0;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM