Sorry for the rather vague title, but I don't know what is wrong with my code.
Here is a Ruby version of what I'm trying to do:
def collect_modulos(base, num)
result = []
arr = (1..(num - 1)).to_a
while !arr.empty? do
b = []
b.push(arr[0])
0.upto(num - 1) do |val|
value = (base * b[-1]) % num
break if value == b[0]
b.push(value)
end
result.push(b)
arr = arr - b
end
result
end
In plain English: Given a prime number denominator num, I want to get all the numerators of all simple fractions < 1 sorted by their decimal expansions, i.e., in the case of num == 7, I want the result to be
0.142857 = 1/7,
0.428571 = 3/7,
0.285714 = 2/7,
0.857142 = 6/7,
0.571428 = 4/7,
0.714285 = 5/7,
i.e., I want to return the numerators 1,3,2,6,4,5. In the cases where there's more than one cycle of repeating decimal expansions, the code should create more than one list.
My effort in the Wolfram Language:
collectModulos[base_,num_]:=With[{collection=Range[1,num-1]},
bigList={};
While[Length[collection]>=1,
smallList={collection[[1]]};
While[First[smallList]!=Last[smallList]&&Length[smallList]>=2,
Append[smallList,Mod[base*Last[smallList],num]]];
DeleteDuplicates[smallList];
Append[bigList,smallList];
collection = Select[collection,Not[MemberQ[smallList]]]
];Return[bigList]];
Attempting to evaluate the function collectModulos[10,7]
should yield the result {1,3,2,6,4,5}
, and collectModulos[14,11]
should yield {{1, 3, 9, 5, 4}, {2, 6, 7, 10, 8}}
, but instead results in the following message:
Set::setraw: Cannot assign to raw object {1,2,3,4,5,6}.
Set::setraw: Cannot assign to raw object {1,2,3,4,5,6}.
Set::setraw: Cannot assign to raw object {1,2,3,4,5,6}.
General::stop: Further output of Set::setraw will be suppressed during this calculation.
The evaluation also gets stuck in an endless loop.
UPDATE: Since this post asked about an error message and wasn't about the code itself, really, it seems the only way I could let people know what I ended up doing was to post my corrected code here as an edit:
This works as intended.
createLists[num_, base_: 10] :=
Module[{collection = Range[1, num - 1], bigList = {}},
If[Mod[base, num] == 0, Return[Nothing]];
While[Length[collection] >= 1,
smallList = {collection[[1]]};
flag = 0;
While[flag == 0,
newValue = Mod[base*Last[smallList], num];
If[newValue == First[smallList], flag = 1,
AppendTo[smallList, newValue]]
];
AppendTo[bigList, smallList];
collection = Select[collection, Not[MemberQ[smallList, #]] &];
]; bigList;
collection
. When you useWith
to localize a variable, you need to treat it as constant. UseModule
instead, unless you understand the difference. $\endgroup$Module
I'm just getting empty lists. But: progress! $\endgroup$