[Jprogramming] Primary Arithmetic
R.E. Boss
r.e.boss at planet.nl
Sun Nov 13 03:17:18 HKT 2005
Oops, made a mistake. Correction follows.
R.E. Boss
-----Oorspronkelijk bericht-----
Van: programming-bounces at jsoftware.com
[mailto:programming-bounces at jsoftware.com] Namens R.E. Boss
Verzonden: zaterdag 12 november 2005 20:10
Aan: 'Programming forum'
Onderwerp: RE: [Jprogramming] Primary Arithmetic
Well, if I look at http://acm.uva.es/p/v100/10035.html (which is almost
equal to http://www.techkriti.org/~iopc/codewars2005/Problem8.html ), I
read: "(...) the "carry" operation - in which a 1 is carried from one digit
position to be added to the next - (...).", then I don't agree that the
number of carry operations of 9+8+7+6+5 is 3.
And http://en.wikipedia.org/wiki/Carry defines "In elementary arithmetic a
carry is a digit that is transferred from one column of digits to another
column of more significant digits during a calculation algorithm".
I think it should read "(...) a carry is the number that is transferred
...", but the number of carry operations of 9+8+7+6+5 is 1. As is the number
of carry operations of +/12#9 .
Here is my solution
digits=: 10&#.^:_1
([:{:[:((9:<+/@[+{.@])([,(+{:))])/0:,~|:)digits 9 8 7 6 5
1
([:{:[:((9:<+/@[+{.@])([,(+{:))])/0:,~|:)digits 12#9
1
([:{:[:((9:<+/@[+{.@])([,(+{:))])/0:,~|:)digits 923923 49049
2
([:{:[:((9:<+/@[+{.@])([,(+{:))])/0:,~|:)digits 987 345 123
3
R.E. Boss
-----Oorspronkelijk bericht-----
Van: programming-bounces at jsoftware.com
[mailto:programming-bounces at jsoftware.com] Namens June Kim
Verzonden: zaterdag 12 november 2005 3:03
Aan: Programming forum
Onderwerp: Re: [Jprogramming] Primary Arithmetic
On 11/12/05, Henry Rich <HenryHRich at nc.rr.com> wrote:
[snip]
>
> carryvec =: carry/
>
> and then use
>
> carryvec 999 1
>
> To extend to more than 2 numbers, I'd need to understand just what
> a 'carry' is in that context.
>
> Henry Rich
Thank you.
I would like to see
carryvec 9 8 7 6 5
and the result be 3. Think about laying down the numbers vertically
and adding them(as you were in the elementary school). The result is,
the sum of magnitude of carries that occur in the whole addition.
I can apply the natural generalization of my algorithm:
9 %~ (dsum 9) + (dusm 8) + (dusm 7) + (dusm 6) + (dusm 5) - dsum 9+8+7+6+5
and get 3.
So how do I change the function defitions and get this "generalization"?
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
More information about the Programming
mailing list