The complete number should be divisible by 9

No need to test this part. All combinations of the digits 1-9 are divisible by nine, since their total is divisible by 9.

Actually, let's see if we can solve this by hand (except for the /7 bit because I'm lazy on that front) as an aside, using standard regexp notation and known rules...

Since even numbers must be divisible by 2, and five must be in the middle (5 and 0 are only valid numbers for position 5), we start with:

[1379][2468][1379][2468]5[2468][1379][2468][1379]

First 4 digits must be divisible by 4, so 3rd and 4th digits must be divisible by 4, ie one of 12,16,32,36,72,76,92 or 96, making it:

[1379][2468][1379][26]5[2468][1379][2468][1379]

Each block of 3 digits must be divisible by 3 (implied since first 3 divisible by 3,6 by 6 and 9 by 9, and sum of digits of frst 3,6 and 9 must also be divisible by 3.This leads us to:

[1379][2468][1379](258|654)[1379][2468][1379]

So we have two cases:

[1379][46][1379](258)[1379][46][1379] [1379][28][1379](654)[1379][28][1379]

Examining the first case now, the first 3 digits can be:

147, 369, 741 and 963

So, what can the 6th to 8th digits be if it has to start with an 8? Well, that means the 7th and 8th digits are divisible by 8:

816 and 896

But 816 can't work because it clashes with all possibilities for the first 3 digits, so we can only try 896, leaving us with, by elimination:

(147|741)(25896)3 ie 147258963 and 741258963
And we haven't touched division by 7 yet*. A quick bit of mental division (1472589/7 and 7412589/7) tells us that neither of these fit the bill, so on to the second case:

[1379][28][1379](654)[1379][28][1379]

Again, first 3 digits can be:

123,129,321,327,723,729,921,927,183,189,381,387,783,789,981,987

400 is divisible by 8, so consider 6th-8th numbers where 7th and 8th are divisible by 8:

432 and 472

Both these contain 2, so we can narrow first three down to:

183,189,381,387,783,789,981,987

Leaving us with these possible candidates:

189654327 1896543/7 = 270934.714 789654321 7896543/7 = 1128077.57 981654327 9816543/7 = 1402363.29 987654321 9876543/7 = 1410934.71 183654729 1836547/7 = 262363.857 189654723 1896547/7 = 270935.286 381654729 3816547/7 = 545221 <-- we have a winner 981654723 9816547/7 = 1402363.86

Well, that was a good waste of a couple of hours. Time to load up the laptop so I can catch up on work tonight :)

Damn you si_lence for sucking me in!

cLive ;-)

* there is a rule for division by seven, but it's no use here. Chop off last digit, double it and remove total from remaining digits. If this number is divisible by seven, then so is original (repeat as necessary). Whether this is any quicker than just dividing by seven the long way or not is debatable in my eyes though - either way, it's no use until we know the last seven digits anyway...


In reply to Re: Golf the numbers! by cLive ;-)
in thread Golf the numbers! by si_lence

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.