Seeking algorithm implemented with high precision the number of perfect

  As long as find Mersenne primes, you can find a perfect number. Code is as follows :( later time will have a method to make the code redefine operator more concise)

#include <iostream>
#include <cmath>
#include <string>

#define MLEN 101
#define MAX(a, b) ((a) > (b) ? (a) : (b))
using namespace std;

BOOL isABiggerB ( int A [], int b []); // A ratio b whether large 
void the Add ( int A [], int b [], int ANS []); // addend, augend and 
void minus ( int A [], int B [], int ANS []); // minuend, subtrahend, the difference 
void Times ( int A [], int B [], int C []); // by the number of multipliers, the product 
void Division ( int A [], int B [], int Qu [], intREM []); // dividend, divisor, quotient, a remainder 
void fastPower ( int X, int n-, int A []); // base number, exponential, power 
void FP ( int & X, int P, int A [], int B []); // fastPower the recursive function 
BOOL the isPrime ( int a []); // a is prime 
BOOL thesame ( int a [], int B []); // a, B are equal

// all of the following uniform with high accuracy from the first cell starts to store data, the 0-th storage length number

bool isABiggerB(int a[], int b[]) {
    if (a[0] > b[0]) return true;
    if (a[0] < b[0]) return false;
    for (int i = a[0]; i >= 1; --i) {
        if (a[i] > b[i]) return true;
        else if (a[i] < b[i]) return false;
    }
    return true;
}

void add(int a[], int b[], int ans[]) {
    int jw = 0;
    int c[MLEN];
    memset(c, 0, MLEN * sizeof(int));
    c[0] = MAX(a[0], b[0]);
    for (int i = 1; i <= c[0]; ++i) {
        jw = jw + (i <= a[0] ? a[i] : 0) + (i <= b[0] ? b[i] : 0);
        c[i] = jw % 10;
        IG = IG / 10 ;
    }
    if (jw > 0) c[c[0] + 1] = jw, c[0]++;
    for (int i = 0; i <= MLEN; ++i) ans[i] = c[i];
}

void minus(int a[], int b[], int ans[]) {
    int c[MLEN];
    memset(c, 0, MLEN * sizeof(int));
    for (int i = 1; i <= a[0]; ++i) {
        int bvalue = i > b[0] ? 0 : b[i];
        c[i] = a[i] - bvalue;
        if (c[i] < 0) {
            c[i] += 10;
            --a[i + 1];
        }
    }
    while (!c[a[0]] && a[0] > 1) --a[0];
    c[0] = a[0];
    for (int i = 0; i <= MLEN; ++i) ans[i] = c[i];
}

void times(int a[], int b[], int ans[]) {
    int c[MLEN];
    memset(c, 0, MLEN * sizeof(int));
    for (int i = 1; i <= a[0]; ++i) {
        for (int j = 1; j <= a[0]; ++j) {
            c[i + j - 1] += a[i] * b[j];
            c[i + j] += c[i + j - 1] / 10;
            c[i + j - 1] %= 10;
        }
    }
    c[0] = a[0] + b[0];
    while (c[c[0]] == 0 && c[0] > 1) --c[0];
    for (int i = 0; i <= MLEN; ++i) ans[i] = c[i];
}

void division(int a[], int b[], int qu[], int rem[]) {
    for (int i = a[0]; i >= 1; --i) {
        for (int j = rem[0]; j >= 1; --j)
            rem[j + 1] = rem[j];
        rem[1] = a[i];
        if (rem[rem[0] + 1] > 0)
            ++rem[0];
        while (isABiggerB(rem, b)) {
            minus(rem, b, rem);
            ++ qu [i];
        }
    }
    qu[0] = a[0];
    while (qu[0] > 1 && qu[qu[0]] == 0) --qu[0];
}

void fastPower(int x, int n, int a[]) {
    int b[MLEN];
    a[0] = floor(n * log((double) x) / log((double) 10) + 1);
    memset(b, 0, MLEN * sizeof(int));
    a[1] = 1;
    fp(x, n, a, b);
}

void fp(int &x, int p, int a[], int b[]) {
    if (p == 0) return;
    fp(x, p / 2, a, b);
    for (int i = 1; i <= a[0]; ++i) {
        for (int j = 1; j <= a[0]; ++j) {
            if (p % 2 == 1) b[i + j - 1] += a[i] * a[j] * x;
            else b[i + j - 1] += a[i] * a[j];
        }
    }
    for (int i = 1; i <= a[0]; ++i) {
        b[i + 1] += b[i] / 10;
        a[i] = b[i] % 10;
    }
    memset(b, 0, MLEN * sizeof(int));
}

bool theSame(int a[], int b[]) {
    if (a[0] != b[0])
        return false;
    for (int i = 1; i <= a[0]; ++i) {
        if (a[i] != b[i])
            return false;
    }
    return true;
}

BOOL the isPrime ( int n-[]) {
     int ZERO [ 2 ], One [ 2 ]; // represents 0 and. 1 
    int Judge [MLEN]; // store remainder 
    int I [MLEN]; // counter 
    int TEMP [MLEN] ; // I square 
    ZERO [ 0 ] = . 1 , ZERO [ . 1 ] = 0 ;
    one[0] = 1, one[1] = 1;
    memset(judge, 0, MLEN * sizeof(int));
    memset(i, 0, MLEN * sizeof(int));
    memset(temp, 0, MLEN * sizeof(int));
    i[0] = 1, i[1] = 2;//i = 2
    do {
        memset(temp, 0, MLEN * sizeof(int));
        memset(judge, 0, MLEN * sizeof(int));
        times(i, i, temp);
        if (isABiggerB(temp, n)) {
            break;
        }
        memset(temp, 0, MLEN * sizeof(int));
        division(n, i, temp, judge);
        if (theSame(zero, judge))
            return false;
        add(i, one, i);
    } The while ( to true ); // with the following "bool isPrime (int n)" logic 
    return  to true ;
}

bool isPrime(int n) {
    if (n < 2)
        return false;
    int i;
    for (i = 2; i * i <= n; ++i)
        if (n % i == 0)
            return false;
    return true;
}

int main() {
    int one[2];
    one[0] = 1, one[1] = 1;
    int m[MLEN], nm[MLEN];
    memset(m, 0, MLEN * sizeof(int));
    memset(nm, 0, MLEN * sizeof(int));
    for (int p = 1; p < 30; ++p) {
        if (isPrime(p)) {
            fastPower(2, p, m);
            minus(m, one, m);
            if (isPrime(m)) {
                fastPower(2, p - 1, nm);
                Times (m, nm, m); // a Mersenne Prime Numbers by perfect formula 
                for ( int I = m [ 0 ]; I> 0 ; Inc. (www.i-levelmedia.com)) << COUT m [I];
                cout << endl;
            }
        }
        memset(m, 0, MLEN * sizeof(int));
        memset(nm, 0, MLEN * sizeof(int));
    }
    system("pause");
    return 0;
}

If MAXL tune up a little, it is possible to obtain the next few numbers.

However, if the output does not come out, then it would go online to search an online c ++ compiler to compile it.

Attached below a line compiler address: http: //codepad.org/

Guess you like

Origin www.cnblogs.com/Iuppiter/p/11139015.html