Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #15565

Re: [Hardest Maths Statement Puzzle] Toughest Math Statement Question

Message-ID <4F090789.D54@mindspring.com> (permalink)
Date 2012-01-07 22:03 -0500
From pete <pfiland@mindspring.com>
Organization PF
Newsgroups comp.lang.c
Subject Re: [Hardest Maths Statement Puzzle] Toughest Math Statement Question
References <d2f643df-1028-4906-808d-faa935f94fa0@f8g2000prm.googlegroups.com>

Show all headers | View raw


divya bisht wrote:
> 
> Toughest Math Statement Question
> (Source : http://hardest-puzzle.blogspot.com/2011/12/toughest-math-statement-question.html
> )
> 
> I am thinking of a 6-digit number. The sum of the digits is 43.
> 
> And only two of the following three statements about the number are
> true:
> 
> (1) it's a square number,
> (2) it's a cube number, and
> (3) the number is under 500000.

/* BEGIN digit_sum_test.c output */

499849 is a 6 digit number.
The sum of the digits of 499849 is 43.
499849 is less than 500000.
499849 is the square of 707.
499849 is not a cube number.

/* END digit_sum_test.c output */




/* BEGIN digit_sum_test.c */
/*
** 999970      cube root == 99, sqrt == 999
** 529999      cube root == 80, sqrt == 728
**
** 499993      cube root == 79, sqrt == 707
** 169999      cube root == 55, sqrt == 412
*/
#include <stdio.h>
#include <stdlib.h>

#define NMEMB(A) (sizeof (A) / sizeof *(A))

unsigned isqrt(unsigned n, unsigned *remainder);
unsigned digit_sum(unsigned n);
int compar(const void*a, const void*b);

int 
main(void)
{
    unsigned bc[99 - 80 + 1];
    unsigned sc[79 - 55 + 1];
    unsigned n;
    unsigned *p;
    unsigned sqrt;
    unsigned rem;
    unsigned square;

    p = bc + NMEMB(bc);
    n = 99;
    do {
        *--p = n * n * n;
    } while (n-- != 80);
    p = sc + NMEMB(sc);
    do {
        *--p = n * n * n;
    } while (n-- != 55);
    puts("/* BEGIN digit_sum_test.c output */\n");
    for (n = 0; n != NMEMB(bc); ++n) {
        sqrt = isqrt(bc[n], &rem);
        if (!rem && digit_sum(bc[n]) == 43) {
            printf("%u is a 6 digit number.\n", bc[n]);
            printf("The sum of the digits of %u is %u.\n",
                bc[n], digit_sum(bc[n]));
            printf("%u is the cube   of %u.\n", bc[n], 80 + n);
            printf("%u is the square of %u.\n", bc[n], sqrt);
            printf("%u is not less than 500000.\n", bc[n]);
        }
    }
    for (n = 0; n != NMEMB(sc); ++n) {
        sqrt = isqrt(sc[n], &rem);
        if (rem && digit_sum(sc[n]) == 43) {
            printf("%u is a 6 digit number.\n", sc[n]);
            printf("The sum of the digits of %u is %u.\n",
                sc[n], digit_sum(sc[n]));
            printf("%u is less than 500000.\n", sc[n]);
            printf("%u is the cube of %u.\n", sc[n], 55 + n);
            printf("%u is not a square number.\n", sc[n]);
        }
    }
    n = 412;
    do {
        square = n * n;
        if (digit_sum(square) == 43 
            && bsearch(&square, sc, NMEMB(sc), sizeof *sc, compar)
            == NULL) 
        {
            printf("%u is a 6 digit number.\n", square);
            printf("The sum of the digits of %u is %u.\n",
                square, digit_sum(square));
            printf("%u is less than 500000.\n", square);
            printf("%u is the square of %u.\n", square, n);
            printf("%u is not a cube number.\n", square);
        }
    } while (n++ != 707);
    puts("\n/* END digit_sum_test.c output */");
    return 0;
}

unsigned 
isqrt(unsigned n, unsigned *remainder)
{
    unsigned power_of_4, root;

    power_of_4 = ((unsigned)-1 >> 2 - (unsigned)-1 % 3) + 1;
    root = 0;
    do {
        if (n >= root + power_of_4) {
            n -= root + power_of_4;
            root += power_of_4 << 1;
        }
        root >>= 1;
        power_of_4 >>= 2;
    } while (power_of_4 != 0);
    if (remainder != NULL) {
        *remainder = n;
    }
    return root;
}

unsigned 
digit_sum(unsigned n)
{
    unsigned sum = 0;
    unsigned tenth = n / 10;

    while (n != 0) {
        sum += n - 10 * tenth;
        n = tenth;
        tenth /= 10;
    }
    return sum;
}

int 
compar(const void *a, const void *b)
{
    const unsigned *aa = a;
    const unsigned *bb = b;

    return *bb > *aa ? -1 : *bb != *aa;
}

/* END digit_sum_test.c */

-- 
pete

Back to comp.lang.c | Previous | Next — Next in thread | Find similar | Unroll thread


Thread

Re: [Hardest Maths Statement Puzzle] Toughest Math Statement Question pete <pfiland@mindspring.com> - 2012-01-07 22:03 -0500
  Re: [Hardest Maths Statement Puzzle] Toughest Math Statement Question Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-26 10:11 -0800
    Re: [Hardest Maths Statement Puzzle] Toughest Math Statement Question pete <pfiland@mindspring.com> - 2012-01-27 17:06 -0500
    Re: [Hardest Maths Statement Puzzle] Toughest Math Statement Question pete <pfiland@mindspring.com> - 2012-01-27 18:30 -0500
      Re: [Hardest Maths Statement Puzzle] Toughest Math Statement Question 88888 Dihedral <dihedral88888@googlemail.com> - 2012-01-31 06:19 -0800
        Re: [Hardest Maths Statement Puzzle] Toughest Math Statement Question Willem <willem@toad.stack.nl> - 2012-03-08 22:26 +0000
      Re: [Hardest Maths Statement Puzzle] Toughest Math Statement Question Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-31 22:28 -0800
        Re: [Hardest Maths Statement Puzzle] Toughest Math Statement Question pete <pfiland@mindspring.com> - 2012-02-01 18:47 -0500
          Re: [Hardest Maths Statement Puzzle] Toughest Math Statement Question Tim Rentsch <txr@alumni.caltech.edu> - 2012-03-08 12:24 -0800

csiph-web