Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.forth > #41829
| From | "WJ" <w_a_x_man@yahoo.com> |
|---|---|
| Newsgroups | comp.lang.forth |
| Subject | Re: Strings and numbers |
| Date | 2015-09-07 19:34 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <mskosf$1tr$1@dont-email.me> (permalink) |
| References | <d0c75445-60e3-402a-93a5-8f68501b28ba@googlegroups.com> <87egjjk8kn.fsf@jester.gateway.sonic.net> <bc852ff3-f547-4e0c-ab68-72b3e9c25618@googlegroups.com> <mq1tq2$jv0$1@dont-email.me> <msccfm$c7p$1@dont-email.me> |
WJ wrote:
> WJ wrote:
>
> > Steve Graham wrote:
> >
> > >
> > > http://programmingpraxis.com/2015/07/21/a-number-puzzle/#comments
> >
> >
> >
> > > Find a 10-digit number, with all digits unique, such that the first n
> > > digits of the number are divisible by n. For instance, in the 3-digit
> > > number 345, the 1-digit prefix, 3, is divisible by 1, the 2-digit
> > > prefix, 34, is divisible by 2, and the 3-digit prefix, 345, is
> > > divisible by 3.
> >
> > Gauche Scheme:
> >
> > (define (next divisor integer digits)
> > (filter-map
> > (lambda (d)
> > (let1 new-int (+ (* 10 integer) d)
> > (and (zero? (mod new-int divisor))
> > (list new-int (delete d digits)))))
> > digits))
> >
> >
> > ;; Recursion.
> > (let go ((divisor 1) (candidates (list (list 0 (iota 10)))))
> > (if (> divisor 10)
> > (caar candidates)
> > (go (+ divisor 1)
> > (append-map
> > (lambda (x) (apply next divisor x))
> > candidates))))
> >
> > 3816547290
>
> Ruby:
>
> def next_digit( divisor, digits_so_far )
> number_so_far = digits_so_far.join.to_i * 10
> ((0..9).to_a - digits_so_far).select{|d|
> ((number_so_far + d).modulo divisor).zero? }.
> map{|d| digits_so_far + [d]}
> end
>
> candidates = [[]]
> 1.upto(10){|divisor|
> candidates = candidates.map{|digit_list|
> next_digit( divisor, digit_list) }.flatten(1) }
> candidates
>
> ===>
> [[3, 8, 1, 6, 5, 4, 7, 2, 9, 0]]
No one has yet posted a Forth solution. Perhaps this Ruby version
will be more understandable than the previous one.
def divisible( n, divisor )
n.modulo(divisor).zero?
end
def one_more_digit( divisor, digits_so_far )
digits_avail = (0 .. 9).to_a - digits_so_far
base_for_new_number = digits_so_far.join.to_i * 10
digits_avail.select{ |d| divisible( base_for_new_number + d, divisor )}.
map{|d| digits_so_far + [d] }
end
Here's an example of how this function works:
one_more_digit( 4, [3, 8, 1] )
==>[[3, 8, 1, 2], [3, 8, 1, 6]]
candidates = [[]]
1.upto(10){|divisor|
candidates = candidates.map{|digit_list|
one_more_digit( divisor, digit_list) }.flatten(1) }
candidates
[[3, 8, 1, 6, 5, 4, 7, 2, 9, 0]]
Here's an example of how the "flatten" command works in Ruby:
[[[3, 8, 1, 2], [3, 8, 1, 6]]].flatten
==>[3, 8, 1, 2, 3, 8, 1, 6]
[[[3, 8, 1, 2], [3, 8, 1, 6]]].flatten(1)
==>[[3, 8, 1, 2], [3, 8, 1, 6]]
Back to comp.lang.forth | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Strings and numbers Steve Graham <solitary.wanderer52@gmail.com> - 2015-08-03 13:24 -0700
Re: Strings and numbers Julian Fondren <julian.fondren@gmail.com> - 2015-08-03 13:51 -0700
Re: Strings and numbers "Rod Pemberton" <boo@fasdfrewar.cdm> - 2015-08-03 17:38 -0400
Re: Strings and numbers Steve Graham <solitary.wanderer52@gmail.com> - 2015-08-03 14:44 -0700
Re: Strings and numbers Paul Rubin <no.email@nospam.invalid> - 2015-08-03 20:59 -0700
Re: Strings and numbers Steve Graham <solitary.wanderer52@gmail.com> - 2015-08-04 12:19 -0700
Re: Strings and numbers "WJ" <w_a_x_man@yahoo.com> - 2015-08-07 09:29 +0000
Re: Strings and numbers "WJ" <w_a_x_man@yahoo.com> - 2015-09-04 15:14 +0000
Re: Strings and numbers "WJ" <w_a_x_man@yahoo.com> - 2015-09-07 19:34 +0000
Re: Strings and numbers Julian Fondren <julian.fondren@gmail.com> - 2015-09-08 11:48 -0700
Re: Strings and numbers Paul Rubin <no.email@nospam.invalid> - 2015-09-09 00:41 -0700
Re: Strings and numbers Richard Todd <richard.wesley.todd@gmail.com> - 2015-09-10 00:32 -0700
Re: Strings and numbers Richard Todd <richard.wesley.todd@gmail.com> - 2015-09-10 10:29 -0700
Re: Strings and numbers Paul Rubin <no.email@nospam.invalid> - 2015-09-10 17:22 -0700
Re: Strings and numbers Richard Todd <richard.wesley.todd@gmail.com> - 2015-09-10 21:34 -0700
Re: Strings and numbers humptydumpty <ouatubi@gmail.com> - 2015-09-11 02:16 -0700
Re: Strings and numbers humptydumpty <ouatubi@gmail.com> - 2015-09-11 07:33 -0700
Re: Strings and numbers Paul Rubin <no.email@nospam.invalid> - 2015-09-13 00:12 -0700
Re: Strings and numbers humptydumpty <ouatubi@gmail.com> - 2015-09-13 02:09 -0700
Re: Strings and numbers Paul Rubin <no.email@nospam.invalid> - 2015-09-11 15:53 -0700
Re: Strings and numbers Andrew Haley <andrew29@littlepinkcloud.invalid> - 2015-12-18 16:02 -0600
Re: Strings and numbers "WJ" <w_a_x_man@yahoo.com> - 2015-09-12 23:38 +0000
Re: Strings and numbers "WJ" <w_a_x_man@yahoo.com> - 2015-09-13 19:41 +0000
Re: Strings and numbers "WJ" <w_a_x_man@yahoo.com> - 2015-11-29 18:09 +0000
csiph-web