标题:求助,用递归的方法求Pell's equation p^2-2q^2=1的前50对解
只看楼主
楚子航992
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2020-5-4
得分:0 
回复 7楼 rjsp
#include<stdio.h>
#define M 10000

void multiply(int x[], int y, int z[]) {
    int i, temp, b = 0;
    for (i = M - 1; i >= 0; i--)
    {
        temp = x[i] * y + b;
        z[i] = temp % 10;
        b = temp / 10;
    }
}

void add(int a[], int b[], int c[]) {
    int i, temp, k = 0;
    for (i = M - 1; i >= 0; i--) {
        temp = a[i] + b[i] + k;
        c[i] = temp % 10;
        k = temp / 10;
    }

}

void copy(int x[], int y[]) {
    int i, j;
    for (i = 0; i < M; i++) {
        if (x[i] > 0) {
            j = i;
            break;
        }

    }
    for (i = j; i < M; i++) {
        y[i] = x[i];
    }
}


void pell(int i, int a[], int b[], int a1[], int a2[], int b1[], int b2[]) {
    int j, m, n;
    if (i == 0) { printf("end"); }
    else {
        multiply(a, 3, a2);
        multiply(b, 4, b2);
        add(a2, b2, a1);
        multiply(a, 2, a2);
        multiply(b, 3, b2);
        add(a2, b2, b1);
        copy(a1, a);
        copy(b1, b);
        for (j = 0; j < M; j++) {
            if (a1[j] > 0)
            {
                m = j;
                break;
            }
        }
        printf("(");
        for (n = m; n < M; n++)
            printf("%d", a1[n]);
        printf(", ");
        for (j = 0; j < M; j++)
        {
            if (b1[j] > 0)
            {
                m = j;
                break;
            }
        }
        for (n = m; n < M; n++)
            printf("%d", b1[n]);
        printf(")");
        printf("\n");
        pell(i - 1, a, b, a1, a2, b1, b2);
    }

}

int main() {
    int a[M] = { 0 }, b[M] = { 0 };
    a[M - 1] = 3;
    b[M - 1] = 2;
    int a1[M] = { 0 }, a2[M] = { 0 }, b1[M] = { 0 }, b2[M] = { 0 };
    printf("(p, q):\n");
    printf("(3, 2)\n");
    pell(49, a, b, a1, a2, b1, b2);
    fflush(stdin);
    getchar();
    return 0;
}
这个可以,秒出
2020-05-31 19:12
楚子航992
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2020-5-4
得分:0 
回复 9楼 forever74
#include<stdio.h>
#define M 10000

void multiply(int x[], int y, int z[]) {
    int i, temp, b = 0;
    for (i = M - 1; i >= 0; i--)
    {
        temp = x[i] * y + b;
        z[i] = temp % 10;
        b = temp / 10;
    }
}

void add(int a[], int b[], int c[]) {
    int i, temp, k = 0;
    for (i = M - 1; i >= 0; i--) {
        temp = a[i] + b[i] + k;
        c[i] = temp % 10;
        k = temp / 10;
    }

}

void copy(int x[], int y[]) {
    int i, j;
    for (i = 0; i < M; i++) {
        if (x[i] > 0) {
            j = i;
            break;
        }

    }
    for (i = j; i < M; i++) {
        y[i] = x[i];
    }
}


void pell(int i, int a[], int b[], int a1[], int a2[], int b1[], int b2[]) {
    int j, m, n;
    if (i == 0) { printf("end"); }
    else {
        multiply(a, 3, a2);
        multiply(b, 4, b2);
        add(a2, b2, a1);
        multiply(a, 2, a2);
        multiply(b, 3, b2);
        add(a2, b2, b1);
        copy(a1, a);
        copy(b1, b);
        for (j = 0; j < M; j++) {
            if (a1[j] > 0)
            {
                m = j;
                break;
            }
        }
        printf("(");
        for (n = m; n < M; n++)
            printf("%d", a1[n]);
        printf(", ");
        for (j = 0; j < M; j++)
        {
            if (b1[j] > 0)
            {
                m = j;
                break;
            }
        }
        for (n = m; n < M; n++)
            printf("%d", b1[n]);
        printf(")");
        printf("\n");
        pell(i - 1, a, b, a1, a2, b1, b2);
    }

}

int main() {
    int a[M] = { 0 }, b[M] = { 0 };
    a[M - 1] = 3;
    b[M - 1] = 2;
    int a1[M] = { 0 }, a2[M] = { 0 }, b1[M] = { 0 }, b2[M] = { 0 };
    printf("(p, q):\n");
    printf("(3, 2)\n");
    pell(49, a, b, a1, a2, b1, b2);
    fflush(stdin);
    getchar();
    return 0;
}
帮忙看下我这么编可以么
2020-05-31 19:14



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-501882-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.247944 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved