sumofsquares {exams.forge} | R Documentation |
Sum of Squared Integers
Description
Decomposes an integer n
into a sum of squared integers (n = \sum_{i=1}^k x_i^2
; 1\leq x_i <n
)
with k\leq nmax
.
If zerosum
is true then it is ensured that \sum_{i=1}^k c_i x_i = 0
with c_i=-1
or c_i=+1
.
The computation of the x_i
's is limited by maxt
seconds, which may result that not all possible
solutions are found. To reduce computing time, rbind
's in the function are replaced by allocating
matrices with size
rows to fill in the results.
Note that the following data sets are available:
-
sos100=sumofsquares(100, 10, zerosum=TRUE, maxt=Inf)
, -
sos200=sumofsquares(200, 10, zerosum=TRUE, maxt=Inf)
, -
sos400=sumofsquares(400, 10, zerosum=TRUE, maxt=Inf)
, -
sos800=sumofsquares(800, 10, zerosum=TRUE, maxt=Inf)
, and -
sos1000=sumofsquares(100, 10, zerosum=TRUE, maxt=Inf)
Usage
sumofsquares(n, nmax = 10, zerosum = FALSE, maxt = 30, size = 100000L)
sum_sq(n, nmax = 10, zerosum = FALSE, maxt = 30, size = 100000L)
Arguments
n |
integer: number to decompose as sum of squares |
nmax |
integer: maximum number of squares in the sum |
zerosum |
logical: should the solution sum up to one (default: |
maxt |
numeric: maximal number of seconds the routine should run |
size |
numeric: length of additional matrix size (default: |
Value
A matrix with nmax
column with x_i
's. NA
means number has not been used.
Examples
sos <- sumofsquares(100, 6) # 23 solutions
head(sos)
table(rowSums(!is.na(sos)))
# one solution with one or two x_i
# five solutions with four x_i
# six solutions with five x_i
# ten solutions with six x_i
rowSums(sos^2, na.rm=TRUE) # all 100
sos <- sumofsquares(100, 6, zerosum=TRUE)
head(sos)
rowSums(sos^2, na.rm=TRUE) # all 100
rowSums(sos, na.rm=TRUE) # all 0