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:

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: FALSE)

maxt

numeric: maximal number of seconds the routine should run

size

numeric: length of additional matrix size (default: 100000L)

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 

[Package exams.forge version 1.0.10 Index]