I used data=Table[{i,f[i]},{i,1,n}]
to produce a list, here n
is greater than 2^20 = 1048576.
The function f(N) runs in time O(N*Log(N))
, it is defined as:
Mod[PowerMod[i,n,n]-i,n] (n is an argument in a function which use this)
Now I want to give a table which shows the values of i that f(i) is 0, and another table for f(i) non-zero.
I used zero = Select[data,#[[2]]==0&]
, but it is slow in the following sense:
n=2^22, timing for data = 10.171, timing for zero = 4.508
n=2^23, timing for data = 21.606, timing for zero = 9.250
n=2^24, timing for data = 43.399, timing for zero = 17.971
n=2^25, timing for data = 84.209, timing for zero = 34.523
n=2^26, timing for data = 167.420, timing for zero = 71.885
The hardest computation is the data
, But after that I want to have a much faster way to know the zeros of the function f
.
Of course I can use For
or Do
to append the zeros i
each time f(i) is zero. But we know that AppendTo
is slow, and For
or Do
is slower than Table
.
Is there any way to construct a list + exact data fast?
Update:
Thanks for all the suggestion. Here is a table of comparison.
The green columns is to find i such that f[i]=0
and the white columns (excluding the 1st and 2nd column) is to find i such that f[i]!=0
. The last 2 columns are in fact using "NonzeroPositions" (the last column) as mentioned by ubpdqn, then do the complement (the second last column). This method is faster.
f[i]
? And how can you take#[[3]]
whileLength@data[[1]]
is2
? $\endgroup$