Hi Victor, > On 13 August 2012 15:05, Ralph Corderoy <ra...@inputplus.co.uk> wrote: > > $ by3() { > > > sed -rn ' > > > h; s/[^147258]//g; y/147258/aaabbb/ > > > :l; s/ab//g; s/ba//g; tl > > > G; s/^\n//p > > > ' "$@" > > > } > > > > I must have missed this conversation.
Yes, me and Tim before your arrival, I think. > I looked at your script and thought "divisibke by 3? That's true if > sum of digits is divisible by three, isn't it?" Right, that's the one where if the http://en.wikipedia.org/wiki/Digital_root of the number is 0, 3, 6, or 9 then it's a multiple of 3 and is the first one of the two listed at http://en.wikipedia.org/wiki/Divisibility_rule#Divisibility_rules_for_numbers_1.E2.80.9320. I was using the second one which I read as if the number of digits that are 1, 4, or 7 is equal to the number that are 2, 5, or 8, then it's a multiple of 3. But on going back for this email I realise that's wrong. It's if the difference between the number of 1/4/7s and 2/5/8s is a *multiple* of 3 then so is the original. It happens that the 1/4/7s and 2/5/8s balance for all multiples up to 100 so the output in my email was correct but it breaks for 111; 3 - 0 = 3. The fix is to the last line of sed; I've widened the test too. :-) $ by3() { > sed -rn ' > h; s/[^147258]//g; y/147258/aaabbb/ > :l; s/ab//g; s/ba//g; tl > G; s/^(...)*\n//p > ' "$@" > } $ $ diff <(seq -99 3 100000) <(seq -100 100000 | by3) $ Cheers, Ralph. -- Next meeting: Dorchester, Tuesday 2012-09-04 20:00 Meets, Mailing list, IRC, LinkedIn, ... http://dorset.lug.org.uk/ New thread on mailing list: mailto:dorset@mailman.lug.org.uk How to Report Bugs Effectively: http://goo.gl/4Xue