[Jprogramming] Splitting an integer into its digits
bill lam
bbill.lam at gmail.com
Sat Aug 28 11:15:39 HKT 2010
to get the oneliner, use the f. adverb eg
(#~ special) f.
digits should be used as rank 0, and the @ adjacent to digits should then
be changed @: otherwise fp1 and +/ would also become used as rank 0.
or you want a spoiler?
Сбт, 28 Авг 2010, Alex Gian писал(а):
> Hi all,
>
> OK, based on the understanding I've gained so far, here and on the other
> thread (re filtering), I thought I'd try a couple of little tests, see
> how I get along.
>
> I've got two here, the first is a quasi trivial programming challenge
> (it was originally given for programmable calculators) and is fairly
> easy in concept. The second one is the so-called "happy numbers" which
> I found quite simple to do, but I may still ask for some style feedback.
>
> First off, the "challenge". It's quite simple in concept, and I know
> how to do all the separate bits in J, but I'm having trouble stringing
> it all coherently. I get "length errors", which makes me suspect rank.
>
> The challenge:
> We define a 'special' number as one which returns itself when subjected
> to the following operation: first split it into its digits, then
> perform the following operation on each digit x: x! + x
> Then sum the result.
> Required to find all special numbers between 1 and 100000 (I think there
> are two)
>
> Here's what I got so far. Not nearly brave enough to put it on one line
> yet.
>
> digits =. 10&#.inv NB. No probs here
> fp1 =. ! + ] NB. I think that's fine too
> spsum =. +/ @ fp1 @ digits NB. Works... BUT...
> special=. ]=spsum
> (#~ special) >:i.1000000 NB. That's the idea, but it's wrong
>
> I think the problem is with spsum, above.
> it will not process arrays correctly, although it's fine with single
> numbers.
>
> And if anyone feels like depriving me of sleep, please write down the
> single line expression...
>
>
>
>
>
>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
--
regards,
====================================================
GPG key 1024D/4434BAB3 2008-08-24
gpg --keyserver subkeys.pgp.net --recv-keys 4434BAB3
More information about the Programming
mailing list