Bruno -
Here's an example of how I'd work this out. I realize I'm not solving
exactly what
you want but you should be able to figure out how to modify it - I see that
you have
what looks like basically the right idea.
First off, I find explicit notation more comprehensible, so I usually start
with that
and use "13 : " to convert to tacit.
numDblSameDig=: 13 : '(":y) *./ . e. ":+:y' NB. Ensure that each digit
of y is found in its double...
numDblSameDig 12 NB. Test it for a
false case. Since I can't think of a true
0 NB.
one off the top of my head, apply it to 1e5 numbers.
6!:2 'whsd=. numDblSameDig &> >:i.1e5'
0.5728932
$whsd
100000
+/whsd NB. How many did we find?
978
(>:i.1e5){~10{.I.whsd NB. What are first 10?
99 663 874 899 989 990 991 999 1255 2551
99,+:99 NB. Check one by eye:
99 198
NB. Not correct because only checks that all digits of y are in 2*y, not the
converse.
NB. Expression to check both ways:
13 : '(x *./ . e. y) *. y *./ . e. x'
*./ .e. *. *./ .e.~
1 9 8 (13 : '(x *./ . e. y) *. y *./ . e. x') 9 9 NB. Test this
sub-expression
0
9 (13 : '(x *./ . e. y) *. y *./ . e. x') 9 9
1
NB. Substitute this into the above definiton to check:
numDblSameDig=: 13 : '(":y) (*./ .e. *. *./ .e.~) ":+:y'
6!:2 'whsd=. numDblSameDig &> >:i.1e5'
1.061952
+/whsd
28
(>:i.1e5){~10{.I.whsd NB. Look at first 10 hits
10255 10525 12505 12550 25051 25105 25501 25510 44897 44987
1 2*10255
10255 20510
So, this works as far as it goes. Obviously, it falls short of what you're
looking
for as it finds only the set of same digits, not the exact same number of
digits as well.
However, you should be able to take it from here to do what you want.
On 7/2/07, Raul Miller <[EMAIL PROTECTED]> wrote:
On 7/2/07, Bruno <[EMAIL PROTECTED]> wrote:
> Hey, what's wrong in this code? I'ts supposed to check if a number and
it's
> double contain the same digits. I tried a couple different ways of
> multiplying a number by two and each way returned me a different
error...
>
> (*/ (/:~ ": ]) = (/:~ ": 2*])) >:i.1e3
> |limit error
(*/ (/:~ ": ]) = (/:~ ": 2*]))
*/ ((/:~ ": ]) = /:~ ": 2 * ])
$ ((/:~ ": ]) = /:~ ": 2 * ]) >:i.1e3
500500
So, here, you're trying to construct a table with 1000 rows and
500500 columns. The limit error is J telling you it's not going to
even bother trying to construct an array that large. (I believe
you are using a 32 bit version of J.)
> (*/ (/:~ ": ]) = (/:~ ": +:])) >:i.1e3
> |domain error
(/:~":+:])>:i.3
|domain error
| (/:~":+:])>:i.3
(/:~ ": +:])
/:~ (": +: ])
Here, you are trying to nor a character string with a set of
integers. The domain of nor is booleans.
I would recommend testing your expressions with smaller
arguments (where you can comfortably inspect the results).
I would also recommend testing your sub-expressions whenever
they wind up doing something you do not expect.
--
Raul
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
--
Devon McCormick, CFA
^me^ at acm.
org is my
preferred e-mail
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm