2

I am trying to calculate two pentagonal numbers whose sum and difference will produce another pentagonal number. In my main function I use pentagonal number theorem to produce pentagonal number sums that produce a pentagonal number and then I check if the difference of those 2 numbers is also pentagonal using is_pentagonal function.

I've written the following code in C++ and for some reason in doesn't give the correct answer and I'm not sure where the mistake is.

The thing is, when I get my answer d then j and k are not pentagonal. j and k simply go above the numerical limit and random numbers eventually produce pentagonal d and i don't get why it happens. Thank you.

bool is_perfect_square(int n) 
{
        if (n < 0) return false;
        int root = sqrt(n);
        return n == root * root;
}

bool is_pentagonal(int n)
{
        if(is_perfect_square(24*n + 1) && (int)sqrt(24*n+1)%6 == 5)return true;
        return false;
}

int main() {
    int j = 0, k = 0, d = 0, n = 1;
    while(!is_pentagonal(d))
    {
        j = (3*n+1)*(3*(3*n+1)-1)/2;
        k = (n*(9*n+5)/2)*(3*n*(9*n+5)/2-1)/2;
        d = k - j;
        ++n;
    }
    cout << d << endl;
    return 0;
}
Andrew
  • 347
  • 1
  • 2
  • 10
  • You say "sum and difference" but I don't see any checks for sum. – Barry Apr 26 '15 at 15:01
  • j+k will produce a pentagonal number according to pentagonal number theorem. so all i need to check is a difference. i checked validity j+k and worked it was fine so i'm pretty sure the problem is not in that bit. – Andrew Apr 26 '15 at 15:03
  • What is the answer you are expecting ? Is the d your are outputting not a pentagonal number ? – Tony Apr 26 '15 at 15:40
  • @Tony When I get my answer, d is pentagonal but j and k aren't. j and k simply go above the numerical limit and random numbers eventually produce pentagonal d and i don't get why it happens. I checked whether j and k are pentagonal up to a numerical limit and they were pentagonal every single time. – Andrew Apr 26 '15 at 15:46
  • Ok. Then, are you sure it is possible to find two such numbers ? If yes, are you sure you span all possible couples with your theorem ? – Tony Apr 26 '15 at 15:48
  • @Tony I used a theorem from there [link](http://www.fq.math.ca/Scanned/8-1/hansen.pdf). Looks like it produces all possible such numbers. – Andrew Apr 26 '15 at 15:55
  • If you refer to theorem 1, I am not quite sure about that. – Tony Apr 26 '15 at 16:03
  • @Tony So you reckon it doesn't produce all such pairs? – Andrew Apr 26 '15 at 16:06
  • @Tony hmm I could try theorem 2 in my program too, however I know that in the answer indeces of pentagonal numbers are very far apart, whereas theorem 2 considers 2 adjacent pentagonal numbers – Andrew Apr 26 '15 at 16:21
  • Well, I guess that if it were the case, it would have been explicitly mentioned. And sorry about theorem 2, went through it a bit fast, I don't think it is relevant here. – Tony Apr 26 '15 at 16:22

1 Answers1

1

I've run this code in ideone:

#include <iostream>
#include <math.h>

using namespace std;

bool is_perfect_square(unsigned long long int n);
bool is_pentagonal(unsigned long long int n);


int main() {

    // I was just verifying that your functions are correct
    /*
    for (int i=0; i<100; i++) {
        cout << "Number " << i << " is pentagonal? " << is_pentagonal(i) << endl;
    }
    */

    unsigned long long int j = 0, k = 0, d = 0;
    int n = 1;
    while(!is_pentagonal(d))
    {
        j = (3*n+1)*(3*(3*n+1)-1)/2;
        if (!is_pentagonal(j)) {
            cout << "Number j = " << j << " is not pentagonal; n = " << n << endl;
        }
        k = (n*(9*n+5)/2)*(3 *n*(9*n+5)/2-1)/2;
        if (!is_pentagonal(k)) {
            cout << "Number k = " << k << " is not pentagonal; n = " << n << endl;
        }
        d = k - j;
        ++n;
     }

    cout << "D = |k-j| = " << d << endl;

    return 0;
}

bool is_perfect_square(unsigned long long int n) {
    if (n < 0)
        return false;
    unsigned long long int root = sqrt(n);
    return n == root * root;
}

bool is_pentagonal(unsigned long long int n)
{
    if(is_perfect_square(24*n + 1) && (1+(unsigned long long int)sqrt(24*n+1))%6 == 0)return true;
    return false;
}

And the result is:

Number k = 18446744072645291725 is not pentagonal; n = 77
Number k = 18446744072702459675 is not pentagonal; n = 78
Number k = 18446744072761861113 is not pentagonal; n = 79
...

If you compare these numbers with 2^64 = 18 446 744 073 709 551 616 as reported at cplusplus.com you will notice you are very close to that. So basically what happens is that your algorithm is correct, but numbers quickly get so big they overflow, and then they are just wrong. See here to check what options you have now.

Community
  • 1
  • 1
  • I looked up the answer online, and the value of k is only supposed to be 7042750. – Andrew Apr 26 '15 at 15:53
  • @Andrew and how are j and k generated in that case? Are they generated as you are doing it? Or are they generated in a different way? Maybe your formula for k is generating only some of the possible values, and you are skipping some - including the one that you want to find. – Fabio says Reinstate Monica Apr 26 '15 at 15:56
  • 1
    I have just checked, you get `k = 6223035` (less than 7042750) for `n = 21` and `k = 7478317` (greater than 7042750) for `n = 22` So defintely you are skipping some numbers. – Fabio says Reinstate Monica Apr 26 '15 at 16:02