primes — Return prime numbers less than or equal to a scalar bound in MATLAB and RunMat.
primes(n) returns a 1-by-N row vector containing every prime number less than or equal to scalar real integer n. Values below 2 return an empty 1-by-0 row vector.
Syntax
p = primes(n)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
n | NumericScalar | Yes | — | Scalar real integer upper bound. |
Returns
| Name | Type | Description |
|---|---|---|
p | NumericArray | Row vector of prime numbers less than or equal to n. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:primes:InvalidInput | Input is not a scalar real integer value. | primes: invalid input |
RunMat:primes:Limit | Requested sieve would exceed RunMat's safety limit. | primes: requested limit is too large |
RunMat:primes:Internal | Internal tensor construction or GPU gather failed. | primes: internal error |
How primes works
nmust be a scalar, finite, real integer value.- Double scalar inputs return a double row vector.
- Single and supported unsigned integer tensor/scalar inputs preserve their output dtype (
single,uint8,uint16, oruint32). - Signed integer and
uint64scalar bounds are accepted and return a double row vector until RunMat tensors include signed integer anduint64array storage. - Logical, complex, non-scalar, non-finite, and fractional inputs raise an error.
- Requests above RunMat's bounded sieve safety limit are rejected before allocation.
Examples
Prime numbers less than or equal to 25
p = primes(25)Expected output:
p = [2 3 5 7 11 13 17 19 23]Prime numbers less than or equal to an unsigned integer
p = primes(uint16(12))Expected output:
p = uint16([2 3 5 7 11])No prime numbers below two
p = primes(1)Expected output:
p = 1x0 empty double row vectorUsing primes with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how primes changes the result.
Run a small primes example, explain the result, then change one input and compare the output.
FAQ
Does primes accept arrays?⌄
No. MATLAB defines n as a scalar upper bound, and RunMat rejects non-scalar inputs.
Why does RunMat reject very large values of n?⌄
The implementation uses a host sieve and rejects requests above its safety limit before allocation to avoid unbounded memory use.
Does primes keep integer output classes?⌄
RunMat preserves supported unsigned integer tensor classes. Signed integer and uint64 array classes are not yet represented by RunMat tensors, so those scalar bounds are accepted and returned as double row vectors for compatibility with common MATLAB call sites.
Related Math functions
Discrete
Elementwise
abs · angle · bsxfun · complex · conj · double · erf · erfcinv · exp · expm1 · factorial · flintmax · gamma · gammaln · heaviside · hypot · idivide · imag · intmax · intmin · ldivide · log · log10 · log1p · log2 · minus · nextpow2 · plus · pow2 · power · rdivide · real · realmax · realmin · realsqrt · rescale · sign · single · sqrt · swapbytes · times · uint32
Trigonometry
acos · acosh · asin · asinh · atan · atan2 · atanh · cos · cosd · cosh · cospi · deg2rad · pol2cart · rad2deg · sin · sind · sinh · sinpi · tan · tand · tanh
Reduction
all · any · bounds · cummax · cummin · cumprod · cumsum · cumtrapz · diff · gradient · max · maxk · mean · median · min · mink · movmax · movmean · movmedian · movmin · movprod · movstd · movsum · movvar · nnz · prod · rms · std · sum · trapz · var
Structure
bandwidth · isdiag · ishermitian · issymmetric · istril · istriu · symrcm
Signal
blackman · butter · buttord · cheb2ord · conv · conv2 · deconv · downsample · envelope · filter · filtfilt · fir1 · freqz · gauspuls · hamming · hann · hilbert · periodogram · pulstran · pwelch · rectpuls · resample · sawtooth · sinc · spectrogram · square · tripuls · unwrap · upsample · zplane
Optim
coneprog · fminbnd · fminunc · fsolve · fzero · integral · linprog · lsqcurvefit · lsqnonlin · optimset · quad · secondordercone
Ops
cross · ctranspose · dot · mldivide · mpower · mrdivide · mtimes · pagemtimes · pagetranspose · trace · transpose
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how primes is executed, line by line, in Rust.
- View the source for primes in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code blazing on any GPU. It is licensed under the Apache 2.0 license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.