TransWikia.com

How to terminate NestWhileList when f is applied to a List

Mathematica Asked by nilo de roock on November 3, 2020

I made the following function to demonstrate a Number Theory theorem:

f[{A_, p_, x_, y_}] := Module[{m, n, z, w, B},
  m = Mod[x, A];
  n = Mod[y, A];
  z = (x m + y n)/A;
  w = (x n - y m)/A // Abs;
  B = (z^2 + w^2)/p;
  {B, p, z, w}
]

The function works fine with NestList

NestList[f, {85, 509, 208, 1}, 3]
(* {{85, 509, 208, 1}, {17, 509, 93, 2}, {4, 509, 44, 10}, {1, 509, 5, 22}} *)

I want to use NestWhileList, show all intermediate results and terminate when the first element of the input list is 1.

I tried the following:

NestWhileList[f, {85, 509, 208, 1}, #[[1]] == 1 &, All]
(* Mod::indet: Indeterminate expression Mod[0,0] encountered. *)

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP