Thread: burmuda triangle strikes again

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    scratch that i just done the calculations. once int sq overflows sq * sq is never = to 1 + 24 * n hence the number is never a pentagonal number

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,644
    Forget this. It's a weird double-post accident....

    A very straightforward way of doing it:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    #define LIMIT 10000000000000000000u // ten quintillion
     
    int main() {
        unsigned long a[20];
        unsigned n = 0, size = sizeof a / sizeof *a;
     
        unsigned long tri  = 1, tri_add  = 2, tri_delta  = 1,
                      pent = 1, pent_add = 4, pent_delta = 3;
        unsigned tri_ind = 1, pent_ind = 1;
    
        printf("triangular and pentagonal:\n");
     
        while (pent < LIMIT) {
            while (tri < pent) {
                tri += tri_add; tri_add += tri_delta; ++tri_ind;
            }
     
            if (tri == pent) {
                printf("%19lu (%10u, %10u)\n", tri, tri_ind, pent_ind);
                if (n >= size) { printf("array overflow\n"); exit(1); }
                a[n++] = tri;
                tri += tri_add; tri_add += tri_delta; ++tri_ind;
            }
     
            while (pent < tri) {
                pent += pent_add; pent_add += pent_delta; ++pent_ind;
            }
     
            if (tri == pent) {
                printf("%19lu (%10u, %10u)\n", tri, tri_ind, pent_ind);
                if (n >= size) { printf("array overflow\n"); exit(1); }
                a[n++] = tri;
                pent += pent_add; pent_add += pent_delta; ++pent_ind;
            }
        }
     
        printf("\nalso hexagonal:\n");
     
        unsigned long hex = 1, hex_add = 5, hex_delta = 4;
        unsigned hex_ind = 1;
     
        for (unsigned i = 0; i < n; ) {
            while (hex < a[i]) {
                hex += hex_add; hex_add += hex_delta; ++hex_ind;
            }
            if (hex == a[i]) printf("%19lu (%10u)\n", hex, hex_ind);
            ++i;
            hex += hex_add; hex_add += hex_delta; ++hex_ind;
        }
     
        return 0;
    }
    
    Output:
    
    triangular and pentagonal:
                      1 (         1,          1)
                    210 (        20,         12)
                  40755 (       285,        165)
                7906276 (      3976,       2296)
             1533776805 (     55385,      31977)
           297544793910 (    771420,     445380)
         57722156241751 (  10744501,    6203341)
      11197800766105800 ( 149651600,   86401392)
    2172315626468283465 (2084377905, 1203416145)
    
    also hexagonal:
                      1 (         1)
                  40755 (       143)
             1533776805 (     27693)
         57722156241751 (   5372251)
    2172315626468283465 (1042188953)
    So every second number that is both triangular and pentagonal is also hexagonal.
    Last edited by john.c; 06-10-2023 at 12:11 PM.
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. codeblocks strikes again
    By cooper1200 in forum C Programming
    Replies: 25
    Last Post: 06-17-2019, 02:01 AM
  2. MS STL strikes again
    By VirtualAce in forum C++ Programming
    Replies: 2
    Last Post: 07-11-2008, 04:56 PM

Tags for this Thread