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

NameTypeRequiredDefaultDescription
nNumericScalarYesScalar real integer upper bound.

Returns

NameTypeDescription
pNumericArrayRow vector of prime numbers less than or equal to n.

Errors

IdentifierWhenMessage
RunMat:primes:InvalidInputInput is not a scalar real integer value.primes: invalid input
RunMat:primes:LimitRequested sieve would exceed RunMat's safety limit.primes: requested limit is too large
RunMat:primes:InternalInternal tensor construction or GPU gather failed.primes: internal error

How primes works

  • n must 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, or uint32).
  • Signed integer and uint64 scalar bounds are accepted and return a double row vector until RunMat tensors include signed integer and uint64 array 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 vector

Using 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.

Discrete

lcm

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

Rounding

ceil · fix · floor · mod · rem · round

Factor

chol · decomposition · eig · eigs · lu · qr · svd

Solve

cond · det · inv · linsolve · norm · null · pinv · rank · rcond · rref · vecnorm

Symbolic

digits · int · limit · piecewise · sym · syms · vpa

Fft

fft · fft2 · fftshift · ifft · ifft2 · ifftshift

Ode

ode15s · ode23 · ode45

Open-source implementation

Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how primes is executed, line by line, in Rust.

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.

Getting started · Benchmarks · Pricing

Download RunMat

Download RunMat for full performance, or use RunMat in your browser for zero setup.