0
$\begingroup$

I have a function that I want to nest the output of into the same function for example I have

p = 47; g = 2; gb = 47;
func[z_, a_, b_] := Which[
  Mod[z, 3] == 0, {g*z, a + 1, b},
  Mod[z, 3] == 1, {gb*z, a, b + 1},
  Mod[z, 3] == 2, {z*z, 2*a, 2*b}];
tort = func[1, 0, 0];
hare = func[func[1, 0, 0]];

Now tort value is tort == {47,0,1} and I wanna reiterate that into my hare variable but i can't figure out how to do it properly

basically feed tort's value into hare and get {13,0,2}

I'm basically trying to implement Pollard Rho for Discrete Log

$\endgroup$

2 Answers 2

2
$\begingroup$

Use Apply (@@) to replace the head of the List with func.

hare = func @@ tort;

Or equivalently:

hare = Apply[func, tort];

Note however that while func[47,0,1] == func @@ tort, it does not equal {13,0,2}. If that's the expected answer, double check your definitions.

For repeated nesting, such as with Nest, it may be useful to write an anonymous function that wraps that, such as func @@ # &:

Nest[func @@ # &, {1, 0, 0}, 2]
$\endgroup$
0
1
$\begingroup$

Another way could be to define the function like func[{z_, a_, b_}] := ...

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.