Project Euler (2)

本次记录的题目是:4, 8, 9, 11, 12, 14, 29, 34, 35, 36。

4.

C++:
#include <iostream>
#include <stdlib.h>
using namespace std;
bool is_palindromic(int number){
    static char index[]="0123456789";
    char str[6] = {NULL};
    int i = 0;
    do{
        str[i++] = index[number%(unsigned)10];
        number /= 10;
    } while(number);
    for (int t = 0; t < i / 2; t++)
        if (str[t] != str[i - t - 1])
            return false;
    return true;
}

int main(int argc, const char * argv[]) {
    int * palindromic = (int *)malloc(sizeof(int) * 10000);
    int x = 0;
    for (int i = 999; i >= 100; i--) {
        for (int t = 999; t >= 100; t--) {
            if (is_palindromic(i*t)) {
                palindromic[x] = i*t;
                x++;
            }
        }
    }
    qsort_b((void *)palindromic, x, sizeof(int), ^int(const void * p1, const void *p2) {
        return *(int*)p2 - *(int*)p1;
    });
    printf("%dn",palindromic[0]);
}

8.

C++:
#include <iostream>
using namespace std;
int main(int argc, const char * argv[]) {
    long long max = 0;
    static const char * digits = "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450";
    for (int pos = 0; pos < 988; pos++) {
        long long num[14] = {NULL};
        num[13] = 1;
        for (int i = 0; i < 13; i++)
            num[13] *= digits[pos + i] - '0';
        if (num[13] > max) max = num[13];
    }
    printf("%lldn",max);
}

9.

C++:
#include <iostream>
using namespace std;
int main(int argc, const char * argv[]) {
    for (long long c = 997; c > 2; c--) {
        for (long long b = c - 1; b > 0; b--) {
            long long a = 1000 - c - b;
            if (a > 0 && c*c == a*a + b*b) {
                printf("%lldn",a*b*c);
                return 0;
            }
        }
    }
}

11.

C++:
#include <iostream>
using namespace std;
int grid[20][20] =
{{ 82, 22, 97, 38, 150, 400, 75457, 78, 52, 12, 50, 77, 918},
{49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 484, 56, 620},
{81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 303, 49, 13, 36, 65},
{52, 70, 95, 234, 60, 11, 42, 69, 24, 68, 561, 32, 56, 71, 372, 36, 91},
{22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80},
{24, 47, 32, 60, 993, 452, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50},
{32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70},
{67, 26, 20, 682, 62, 12, 20, 95, 63, 94, 39, 638, 40, 91, 66, 49, 94, 21},
{24, 55, 585, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72},
{21, 36, 239, 750, 76, 44, 20, 45, 35, 140, 61, 33, 97, 34, 31, 33, 95},
{78, 17, 53, 28, 22, 75, 31, 67, 15, 943, 804, 62, 16, 149, 53, 56, 92},
{16, 395, 42, 96, 35, 31, 47, 55, 58, 88, 240, 17, 54, 24, 36, 29, 85, 57},
{86, 560, 48, 35, 71, 8975, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58},
{19, 80, 81, 685, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 774, 89, 55, 40},
{ 4, 528, 83, 97, 35, 99, 167, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66},
{88, 36, 68, 87, 57, 62, 20, 723, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69},
{ 4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 188, 46, 29, 32, 40, 62, 76, 36},
{20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 744, 36, 16},
{20, 73, 35, 29, 78, 31, 901, 74, 31, 49, 71, 48, 86, 81, 16, 23, 575, 54},
{ 1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 521, 89, 19, 67, 48}};
 
long long max_in_row(int row){
    long long max = 0;
    for (int pos = 0; pos < 17; pos++) {
        long long tmp = 1;
        for (int i = 0; i < 4; i++)
            tmp *= grid[row][pos+i];
        if (max < tmp) max = tmp;
    }
    return max;
}
 
long long max_in_column(int column){
    long long max = 0;
    for (int pos = 0; pos < 17; pos++) {
        long long tmp = 1;
        for (int i = 0; i < 4; i++)
            tmp *= grid[pos+i][column];
        if (max < tmp) max = tmp;
    }
    return 0;
}
 
long long max_in_diagonally(int row, int column, bool direction){
    /*!
     *  @brief  direction
     *
     *  true:    false:
     *     x___     ___x
     *     _x__     __x_
     *     __x_     _x__
     *     ___x     x___
     *
     */
    long long res = 1;
    if (direction)
        for (int x = 0, y = 0; x < 4 && y < 4; x++,y++)
            res *= grid[row + x][column + y];
    else
        for (int x = 0, y = 0; x < 4 && y < 4; x++,y++)
            res *= grid[row + x][column - y];
    return res;
}
 
int main(int argc, const char * argv[]) {
    long long max = 0;
    for (int row = 0; row < 20; row++) {
        long long row_m = max_in_row(row);
        if (max < row_m) max = row_m;
    }
    for (int column = 0; column < 20; column++) {
        long long column_m = max_in_column(column);
        if (max < column_m) max = column_m;
    }
    for (int row = 0; row < 17; row++) {
        for (int column = 0; column < 17; column++) {
            long long diagonally_t_m = max_in_diagonally(row, column, true);
            long long diagonally_f_m = max_in_diagonally(row, column, false);
            if (max < diagonally_t_m) max = diagonally_t_m;
            if (max < diagonally_f_m) max = diagonally_f_m;
        }
    }
    printf("%lldn",max);
}

12.

Mathematica:
    triangular = 1;
    For[step = 2, step < Infinity, step++,
      {If[Length[Divisors[triangular]] >= 500, Break[]],
       triangular += step}
    ];
    Print[triangular];

14.

Mathematica:
    num = 0;
    max = 0;
    For[number = 999999, number > 0, number--,
     {tmp = 0;
      n = number;
      While[n != 1,
       {If[EvenQ[n], n /= 2, n = 3*n + 1], tmp++}
      ],
      If[max < tmp, {max = tmp, num = number}]
      }
    ]
    Print[num]

29.

Mathematica:
    Length[
      DeleteDuplicates[
        ArrayReshape[
          Table[a^b, {a, 2, 100}, {b, 2, 100}],
          {1, 9801}
        ][[1]]
      ]
    ]

34.

C++:
#include <iostream>
using namespace std;
int main(int argc, char *argv[]) {
    static const int factorial[10] = {1,1,2,6,24,120,720,5040,40320,362880};
    long long count = 0;
    // 9! + 9! + 9! + 9! + 9! + 9! + 9! = 2540160
    // However, 9999999 is much bigger than 2540160
    for (int i = 3; i < 2540160; i++) {
        int p = i;
        int res = 0;
        while (p != 0) {
            res += factorial[p % 10];
            p /= 10;
        }
        if (res == i) count+=i;
    }
    printf("%lldn",count);
}

35.

Mathematica:
    Export["primes.txt", Table[Prime[n], {n, 1, 78498}]];
C++:
#include <iostream>
#include <string>
#include <fstream>
#include <map>
using namespace std;

map<int, int> prime_m;
 
int* rotate(const char * prime){
    size_t length = strlen(prime);
    int * result = (int *)malloc(sizeof(int) * length);
    char * tmp = (char *)malloc(sizeof(char) * length * 2);
    for (int pos = 0; pos < length * 2; pos++) {
        tmp[pos] = prime[pos % length];
    }
    for (int offset = 1; offset < length; offset++) {
        char * tmp2 = (char *)malloc(sizeof(char) * length);
        for (int p = 0; p < length; p++)
            tmp2[p] = tmp[offset + p];
        int r_p = atoi(tmp2);
        result[offset-1] = r_p;
        free(tmp2);
    }
    free(tmp);
    return result;
}
 
int main(int argc, const char * argv[]) {
    ifstream primes("primes.txt");
    if(!primes) return 1;
    string prime;
    while(getline(primes, prime))
        prime_m[atoi(prime.c_str())] = 1;
    primes.clear();
    primes.seekg(0, ios::beg);
    int count = 0;
    while(getline(primes, prime)) {
        int * rotations = rotate(prime.c_str());
        int count_i = 0;
        for (int i = 0; i < prime.length() - 1; i++)
            if (prime_m[rotations[i]] == 1) count_i++;
        if (count_i == prime.length() - 1) count++;
    }
    printf("%dn",count);
    return 0;
}

36.

C++:
#include <iostream>
#include <bitset>
using namespace std;
bool is_palindromic_binary(bitset<20> number){
    int start = 19;
    for (; start > 0; start--)
        if (number.test(start)) break;
    for (int i = start; i > (start / 2); i--) {
        if (number.test(i) != number.test(start - i))
            return false;
    }
    return true;
}
 
bool is_palindromic_decimal(int number){
    static char index[]="0123456789";
    char str[7] = {NULL};
    int i = 0;
    do{
        str[i++] = index[number%(unsigned)10];
        number /= 10;
    } while(number);
    for (int t = 0; t < i / 2; t++)
        if (str[t] != str[i - t - 1])
            return false;
    return true;
}
 
int main(int argc, const char * argv[]) {
    int count = 0;
    for (int i = 0; i < 1000000; i++)
        if (is_palindromic_binary(i) && is_palindromic_decimal(i))
            count+=i;
    printf("%dn",count);
}
声明: 本文为0xBBC原创, 转载注明出处喵~

发表评论

电子邮件地址不会被公开。 必填项已用*标注