Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news.glorb.com!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail NNTP-Posting-Date: Fri, 27 Jan 2012 17:30:18 -0600 Message-ID: <4F233391.5FC@mindspring.com> Date: Fri, 27 Jan 2012 18:30:25 -0500 From: pete Reply-To: pfiland@mindspring.com Organization: PF X-Mailer: Mozilla 3.04Gold (WinNT; I) MIME-Version: 1.0 Newsgroups: comp.lang.c Subject: Re: [Hardest Maths Statement Puzzle] Toughest Math Statement Question References: <4F090789.D54@mindspring.com> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 67 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 4.154.217.124 X-Trace: sv3-J6G6wnfHYnEGLsWzWszSca8qM0tc9KF+9YNU85OPKOMuDCSOXknTvUYOWCaTZ1cWfsm1qoIO/xqiXY2!sxoKC7DEljBmAck3ylrZiCk1odWTIQRrhEfIaLDOd/VVTrJz7gXLDS7jWeqFlTDD6wIG7lvs9yNJ!AdjoWiSrAA== X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 3045 Xref: x330-a1.tempe.blueboxinc.net comp.lang.c:16323 Tim Rentsch wrote: > > pete writes: > > > 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. > > > > [snip program -- 114 lines of code, 24 lines of comments] > > Seems like a rather long program for the problem... how > about this instead? > > #include > > int digit_sum_of_equals( unsigned number, unsigned total ); > > int > main(){ > const unsigned SUM = 43; > unsigned i = 999, j = 99, s, c; > /*** 's' is the square of 'i', 'c' is the cube of 'j' ***/ > > printf( "Looking for six digit numbers whose digits total %u...\n", SUM ); > > while( s = i*i, c = j*j*j, s > 99999 || c > 99999 ){ > > if( s > c && s < 500000 && digit_sum_of_equals( s, SUM ) ){ > printf( " %u == %u**2\n", s, i ); > } > > if( c > s && c < 500000 && digit_sum_of_equals( c, SUM ) ){ > printf( " %u == %3u**3\n", c, j ); > } > > if( s == c && s >= 500000 && digit_sum_of_equals( s, SUM ) ){ > printf( " %u == %u**2 == %u**3\n", s, i, j ); > } > > if( s >= c ) i--; > if( c >= s ) j--; > } > > return 0; > } > > int > digit_sum_of_equals( unsigned n, unsigned t ){ > return n == 0 ? t == 0 : digit_sum_of_equals( n/10, t - n%10 ); > } The algorithm seems to be a little more clever. I'll have to study it a bit. -- pete