Question

How do I get this equation to multiply by itself?

*/-.%~.q:36

other than repeating the number 36 again. It's Euler's Totient Function by the way.

enter image description here

I'm lacking the last step of multiplying this by itself.

Yes, I know they already have the code for this function at jsoftware. But I'm trying to break down the codes and learn.

Sorry to ask such simple questions. It's really hard to find help for J on Google.

Was it helpful?

Solution

Short answer

y * (f y)

is

(* f) y

Longer answer

You have a case of

y f (g (y))

where f is the dyad * and g is the function that you already had: */-.%~.q:. Now, y f (g(y)) transforms simply to the "train" (f g) y (as you can see at the manual). Using Cap [:* to parenthesize g:

g =: [: */ [: -. [: % [: ~. q:

we finally have:

phi =: * g
phi 36
12

or

(* [: */ [: -. [: % [: ~. q:) 36
12

* You can use Atop and At to construct function g but Cap is usually clearer for trains.

OTHER TIPS

The most direct way to use the value again is to include the value again.

   */ 36, -. % ~. q: 36
12

A name can be used for the value.

   */ y, -. % ~. q: y=. 36
12

A verb can be defined. The name is local within it.

   etf=: verb : '*/ y, -. % ~. q: y'
   etf 36
12

The same verb can be phrased in tacit form. In this form the parameter is implied, not named. Here the key to using the parameter value twice is the hook created within parentheses.

   etfT=: */ @ ( , -. @ % @ ~. @ q: )
   etfT 36
12

Ok this what I came up with.

(* */@:-.@:%@~.@q:)36

After I played around with this function trying to find out how it works and researching and the like (I only have a second year Calculus background), I looked back at an old tab for the primes function p: and found that J has built in Euler's totient function 5&p: .

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top