When did the usage of "N" to indicate some unspecified quantity come up?
As you suspected, it is thought to come from the mathematical usage:

为什么“N”可以代表“很多”的意思?例如N多次,N多个
Why can &quot;N&quot; have the meaning &quot;a lot&quot;? For instance, N-times, N-of-them
因为数学上的数列一般都用第N项代表任一项,可知无穷大的一项.
Because in math, sequences usually use &quot;the nth term&quot; to represent any term, including unboundedly large ones.

Another (perhaps more reputable) source

...n在代数中表示很多，如从1开始到2,到3,..., 到n。因此在口语中n经常用来表示特别多，例如“买了n多电话卡”...
...in algebra, &quot;N&quot; signifies &quot;a lot&quot;, as in &quot;from 1, 2, 3, up to N&quot;. Because of this, in colloquial speech, &quot;N&quot; often is used to express &quot;very many&quot;, as in &quot;to buy sooo many telephone cards&quot;...