[Jprogramming] Square Roots and Extended Arithmetic

Linda Alvord lindaalvord36 at outlook.com
Sun Nov 11 22:14:27 UTC 2018


Jose, I found this from an early forum. It seems to have a "Y" approach hiding in it.

f=: 13 :',([:+/\|.)^:2^:(<`(1,1:))y'
   f 5
1 1 2 3 5 8 13 21 34 55

Linda


-----Original Message-----
From: Programming <programming-bounces at forums.jsoftware.com> On Behalf Of Skip Cave
Sent: Saturday, November 3, 2018 12:25 AM
To: Programming at jsoftware.com
Subject: Re: [Jprogramming] Square Roots and Extended Arithmetic

Linda,

NB. Generate the integers from 2 to 21, store them in a, and display a:
]a=.2+i.20

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21


NB. Take the square root of the integers in a, store them in b, and display
b:

]b=.%:a

1.41421 1.73205 2 2.23607 2.44949 2.64575 2.82843 3 3.16228 3.31662 3.4641
3.60555 3.74166 3.87298 4 4.12311 4.24264 4.3589 4.47214 4.58258


NB. Find the ceiling of b (which contains all the square roots of a)

>.b

2 2 2 3 3 3 3 3 4 4 4 4 4 4 4 5 5 5 5 5


NB. Mark all the places where the ceiling of b is the same as b, and display the mark vector.

NB. This essentially marks every number in b that has no fractional part, i.e the numbers in b that are integers.

]m=. b = >. b

0 0 1 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0


NB. this is the same as:

(=>.) b

0 0 1 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0

NB. Now we can use the mark vector to display only the numbers in b that are integers:

m#b

2 3 4

NB: Or we can use the mark vector to select all the integers from a that have integer square roots:

m#a

4 9 16


NB. Now, like most J expressions, we can do either of these things all in one line:

b#~(=>.)%:a=.2+i.20

2 3 4


a#~(=>.)%:a=.2+i.20

4 9 16



Skip

Skip Cave
Cave Consulting LLC


On Fri, Nov 2, 2018 at 9:25 PM Linda Alvord <lindaalvord36 at outlook.com>
wrote:

> What does (=>.)  do in this example?
>
> Linda
>
>
>
> Sent from my Verizon, Samsung Galaxy smartphone
>
>
> -------- Original message --------
> From: Skip Cave <skip at caveconsulting.com>
> Date: 11/2/18 12:06 PM (GMT-05:00)
> To: "Programming at jsoftware.com" <programming at jsoftware.com>
> Subject: Re: [Jprogramming] Square Roots and Extended Arithmetic
>
> Thanks for all the comments and help in understanding why trying to 
> get an extended integer result from a square root is a bad idea.
>
> I think the functional symmetry of square *: and square root %: led me 
> to subconsciously forget that the results of squared integers (all 
> integers) are not symmetrical with the results of the square roots of 
> integers (which are NOT all integers). I should remember this, since 
> one of my favorite J idioms is the implicit verb (=>.) for finding 
> integers in a list of floating point numbers:
>
> a=.2+i.20
>
> %:a
>
> 1.41421 1.73205 2 2.23607 2.44949 2.64575 2.82843 3 3.16228 3.31662 
> 3.4641
> 3.60555 3.74166 3.87298 4 4.12311 4.24264 4.3589 4.47214 4.58258
>
> (=>.)%:a
>
> 0 0 1 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0
>
> a#~(=>.)%:a
>
> 4 9 16
>
> Skip
> ----------------------------------------------------------------------
> For information about J forums see
> https://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Ffd.xuwubk.eu.org%3A443%2Fhttp%2Fwww.j
> software.com%2Fforums.htm&data=02%7C01%7C%7C4f696d5065b94098ad1c08
> d641446bd9%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C63676815945102
> 0586&sdata=z54fz6QFIO4PP3iXH93jLlbZaYYL6YiSMlc6laIqwLs%3D&rese
> rved=0
> ----------------------------------------------------------------------
> For information about J forums see 
> https://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Ffd.xuwubk.eu.org%3A443%2Fhttp%2Fwww.j
> software.com%2Fforums.htm&data=02%7C01%7C%7C4f696d5065b94098ad1c08
> d641446bd9%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C63676815945102
> 0586&sdata=z54fz6QFIO4PP3iXH93jLlbZaYYL6YiSMlc6laIqwLs%3D&rese
> rved=0
----------------------------------------------------------------------
For information about J forums see https://eur03.safelinks.protection.outlook.com/?url=https%3A%2F%2Ffd.xuwubk.eu.org%3A443%2Fhttp%2Fwww.jsoftware.com%2Fforums.htm&data=02%7C01%7C%7C4f696d5065b94098ad1c08d641446bd9%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636768159451020586&sdata=z54fz6QFIO4PP3iXH93jLlbZaYYL6YiSMlc6laIqwLs%3D&reserved=0


More information about the Programming mailing list