in reply to Golf the numbers!

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...

Replies are listed 'Best First'.
Re^2: Golf the numbers!
by BrowserUk (Patriarch) on Oct 30, 2004 at 00:18 UTC

    Nice++.

    These are these methods I was taught by dear ol' "Spud" Murphy (otherwise known as Dr.T.N Murphy--we never did find out what the T.N. stood for:). I remember him ruing the day when he had to tell us that we could use calculators in our exams provided that they didn't have memories.

    I borrowed my brother-in-law's Sinclair Scientific (which he wore on his belt to meetings like people did with mobile phones in the late '80s:). I took it into the exam and placed it on the desk in front of me, but then used log tables because I had never got to grips with the reverse polish notation!

    Spud was right about calculators.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon
Re^2: Golf the numbers!
by hv (Prior) on Oct 31, 2004 at 13:19 UTC

    there is a rule for division by seven, but it's no use here ...

    Alternatively, convert to base 8 and add the digits. :)

    More useful can be to take advantage of the fact that 1001 is 7 * 11 * 13 - you can check for divisibility of all three of those in one step by adding/subtracting alternating groups of 3 digits (counting from the right).

    Eg:

    testing 123456789 split into groups: 789, 456, 123 add/subtract alternately: 789 - 456 + 123 = 456 factorise what's left: 456 = 2^3 * 3 * 19 so 123456789 is not divisible by 7 or 11 or 13

    If the resulting number is difficult to factorise in your head, you can always add/subtract multiples of one of those primes until you get to something that obviously is/isn't a multiple of that prime:

    456 = 400 + 56 (so it isn't divisible by 7) 456 = 390 + 66 (so it isn't divisible by 11 or 13)

    Hugo

Re^2: Golf the numbers!
by tilly (Archbishop) on Nov 03, 2004 at 02:10 UTC
    If you're going to do this much work, you can do it entirely by hand.

    Let's see. The fifth number has to be divisible by 5, so that digit is 0 or 5, but we don't have a 0 so that is 5. Digits in position 2, 4, 6, and 8 must be even, so that uses up 4 even digits - we only have 4 so all must be used there. Therefore the odd digits must be odd.

    Oh, but we can go further still, so far we know that at position 4 we have (odd)(even)(odd)(even) and we need it divisible by 4, that will only happen if the 4'th digit is divisible by 2 but not 4. A similar thing happens at position 8. We only have 2 such digits, 2 and 6. Therefore 2 and 6 appear at positions 4 and 8. And 4 and 8 appear at positions 2 and 6.

    We are now in a position to do exhaustive search. The pattern is that we want a permutation where the digits happen to fall into the sequence

    1. 1, 3, 7, 9
    2. 4, 8
    3. 1, 3, 7, 9
    4. 2, 6
    5. 5
    6. 4, 8
    7. 1, 3, 7, 9
    8. 2, 6
    9. 1, 3, 7, 9
    With this pattern we need to check that the first 3 digits are divisible by 3. The second 3 digits are divisible by 3. The first 7 are divisible by 7. And the 7'th and 8'th are divisible by 8. Here is the exhaustive search:
    143 x 14725836 x 1472589 x 147658 x 149 x 183254 x 1836547 x 1836549 x 187 x 189254 x 1896547 x 1896549 x 341 x 347 x 349 x 381254 x 381654729 <--- Yes! 3816549 x 387254 x 3876541 x 3876549 x 389 x 7412583 x 7412589 x 741658 x 743 x 749 x 781 x 783254 x 7836541 x 78365492 x 789254 x 7896541 x 7896543 x 941 x 943 x 947 x 981254 x 9816543 x 9816547 x 983 x 987254 x 9876541 x 9876543 x
    And there you have it, only one answer.

    Incidentally that there would be about one answer should not surprise. After all we have 9! permutations. But odds are that all of them get through the first check, half the second, a third the third, a quarter the fourth, etc. So we'd expect about 1/9! of the permutations to get through. Which (hmmm...) works out an estimate of 1 right answer...

Re^2: Golf the numbers!
by japhy (Canon) on Nov 02, 2004 at 16:33 UTC
    I started writing a mathematical response here, but I've created a new thread in Meditations.
    _____________________________________________________
    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart