Skip to content

Others

CalcpadCE is general enough to cover small algorithmic and combinatorial problems alongside its main engineering workload.

This catch-all group collects eight short worksheets that exercise different parts of the language. Quadratic Equation and Cubic Equation use the built-in numerical solver to recover all real roots from a coefficient list, while Combinatorics tabulates permutations and combinations with and without repetition. Bitwise Operations builds binary, octal and hexadecimal converters from custom multiline functions, and Collatz Check verifies the 3n + 1 conjecture over a user-chosen range using both inline and recursive loops. Functions of Multiple Parameters showcases the variadic min, max, gcd and lcm helpers, Himmelblau Function plots the four-minimum Himmelblau test surface used in optimisation benchmarks, and HTML UI wires raw HTML form controls into a worksheet to drive a parametric cross-section design.

HTML UI

Wires raw HTML \(select\) and input controls into a worksheet to drive a parametric cross-section design, demonstrating Calcpad form-based UI. Copy the code into the Desktop application to modify the input values.

Code:
'Cross section: <select name="section">
'<option value="100;150">RHS 100x150</option>
'<option value="150;200">RHS 150x200</option>
'<option value="200;250">RHS 200x250</option>
'</select>
'<p id="section"> Dimensions:'b = ? {100}mm','h = ? {150}mm'</p>

'<p>Steel grade:
'<input name="steel" type="radio" id="S235" value="235" >
'<label for="S235">S235</label>
'<input name="steel" type="radio" id="S275" value="275" >
'<label for="S275">S275</label>
'<input name="steel" type="radio" id="S355" value="355" >
'<label for="S355">S355</label></p>
'<p id="steel" style="display:none;">'f_y = ? {235}MPa'</p>
'Steel yield strength:'f_y

'Additional options:
'<p><input name="Rolled" type="checkbox" id="roll" value="1">
'<label for="roll">Hot rolled</label></p>
'<p id="Rolled" style="display:none;">'Rolled = ? {1}'</p>
'<p><input name="Zn" type="checkbox" id="zink" value="1" >
'<label for="Zn">Zink plated</label></p>
'<p id="Zn" style="display:none;">'Zinc = ? {1}'</p>
'Values:
Rolled
Zinc
Rendered Output:

Cross section:

Dimensions: b = 100  mm , h = 150  mm

 

Steel grade:

Steel yield strength: fy = 235 MPa

 

Additional options:

Values:

Rolled = 1

Zinc = 1

Collatz Check

Verifies the Collatz (3n + 1) conjecture over a user-chosen range using both inline and recursive iteration.

Code:
"Sample Program to Check the Collatz Conjecture
'<p>For more information see <a href="https://en.wikipedia.org/wiki/Collatz_conjecture">https://en.wikipedia.org/wiki/Collatz_conjecture</a></p>
'<h4>Check by inline loop</h4>
'Function -'CollatzCheck(k; n) = $Repeat{k = switch(k  1; 1; k2  0; k/2; 3*k + 1) @ i = 1 : n}
'Number to check -'k = 27
'Maximum number of steps -'n = 10000
'Check -'CollatzCheck(k; n)
'<h4>Check by block loop</h4>
#def Collatz_Check$(k$; n$)
    #hide
    v = vector_hp(n$)
    i = 0
    k_ = k$
    v.(1) = k$
    #repeat n$
        i = i + 1
        #if k_  1
            i = i - 1
            resize(v; i)
            #break
        #else if k_2  0
            k_ = k_/2
        #else
            k_ = 3*k_ + 1
        #end if
        v.(i + 1) = k_
    #loop
    #show
    #val
    #if k_  1
        '<p>Collatz check for k = <b>'k$'</b> <span class="ok">succeeded</span> after 'i' steps.</p>
    #else
        '<p>Collatz check for k = <b>'k$'</b> <span class="err">failed</span> after 'n$' steps.</p>
    #end if
    #equ
#end def
Collatz_Check$(k; n)
'<h4>Plot of steps <small>- for 'k'</small></h4>
'<!--'PlotHeight = 150','PlotWidth = 350','PlotSVG = 1'-->
$Plot{take(x; v) @ x = 1 : i}
'<h4>List of steps</h4>
#def Collatz_List$(k$; n$)
    #hide
    i = 0
    k_ = k$
    #show
    #val
    '<p>Collatz check for number k = <b>'k$'</b>:</p>
    '<table>
    '<tr style="background:#ddd;"><th> ⚑ </th><th>Start =</th><th>&ensp;'k_'&emsp;</th>
    #if k_2  0
        '<th>even</th</tr>
    #else
        '<th>odd</th</tr>
    #end if
    #repeat n$
        #hide
        i = i + 1
        #show
        #if k_  1
            #hide
            i = i - 1
            #show
            #break
        #else if k_2  0
            '<tr>
            #if k_  1
                '<td class="ok"><b> ⚐ </b></td>
            #else
                '<td class="ok"><b> 🡖 </b></td>
            #end if
            '<td>÷ 2 =</td><td>&ensp;<b>'k_ = k_/2'</b></td>
        #else
            '<tr><td class="err"><b> 🡕 </b></td><td>* 3 + 1 =</td><td>&ensp;<b>'k_ = 3*k_ + 1'</b></td>
        #end if
        #if k_  1
            '<td>end</td></tr>
        #else if k_2  0
            '<td>even</td></tr>
        #else
            '<td>odd</td></tr>
        #end if
    #loop
    '</table><br/>
    #if k_  1
        'Check for k = 'k$' <span class="ok">succeeded</span> after 'i' steps.
    #else
        'Check <span class="err">failed</span> after 'n$' steps.
    #end if
    #equ
#end def
Collatz_List$(11; 1000)
'<h4>Check all numbers in range</h4>
#def Collatz_Check_Between$(k0$; kn$; n$)
    #hide
    k0 = max(k0$; 1)
    i = k0 - 1
    #repeat kn$ - k0 + 1
        i = i + 1
        k_ = CollatzCheck(i; n$)
        #if k_  1
            i = i - 1
            #break
        #end if
    #loop
    #show
    #val
    #if k_  1
        '<p>Collatz check <span class="ok">succeeded</span> for all numbers between 'k0' and 'kn$'.</p>
    #else
        '<p>Collatz check <span class="err">failed</span> for <b>'i'</b> and 'n$' steps.</p>
    #end if
    #equ
#end def
Collatz_Check_Between$(2;1000; n)
Rendered Output:
Sample Program to Check the Collatz Conjecture

For more information see https://en.wikipedia.org/wiki/Collatz_conjecture

Check by inline loop

Function - CollatzCheck ( k; n )  = $Repeat{k = {if k ≡ 1: 1
else if k mod 2 ≡ 0: k2
else: 3 · k + 1
for i = 1...n}

Number to check - k = 27

Maximum number of steps - n = 10000

Check - CollatzCheck  ( k; n )  = CollatzCheck  ( 27; 10000 )  = 1

Check by block loop

Collatz check for k = 27 succeeded after 111 steps.

Plot of steps - for k = 27
0 2000 4000 6000 8000 0 10 20 30 40 50 60 70 80 90 100 110 x [1; 0] [111; 9232]
List of steps

Collatz check for number k = 11:

Start = 11 odd
🡕 * 3 + 1 =34even
🡖 ÷ 2 =17odd
🡕 * 3 + 1 =52even
🡖 ÷ 2 =26even
🡖 ÷ 2 =13odd
🡕 * 3 + 1 =40even
🡖 ÷ 2 =20even
🡖 ÷ 2 =10even
🡖 ÷ 2 =5odd
🡕 * 3 + 1 =16even
🡖 ÷ 2 =8even
🡖 ÷ 2 =4even
🡖 ÷ 2 =2even
🡖 ÷ 2 =1end

Check for k = 11 succeeded after 14 steps.
Check all numbers in range

Collatz check succeeded for all numbers between 2 and 1000.

Quadratic Equation

Solves \(a x^2 + b x + c = 0\) with the built-in numerical root finder, returning both real or complex roots from the user-supplied coefficients.

Code:
f(x) = a*x^2 + b*x + c'= 0
'Coefficients:
a = ? {2}','b = ? {3}','c = ? {-5}
#hide
PlotWidth = 360''PlotHeight = 200
#post
#if a  0
    'The coefficient <i>a</i> is equal to zero. The equation is linear.
    x = -c/b
#else
    'Discriminant:
    D = b^2 - 4*a*c
    #if D  0
        'The discriminant is grater or equal to zero. Real roots exist.
        q = -0.5*(b + sign(b  0 + b)*sqr(D))
        x_1 = q/a', ' _
        x_2 = c/q
        #hide
        d = abs(x_2 - x_1)/4
        x_min = min(x_1; x_2) - d
        x_max = max(x_1; x_2) + d
        #post
        $Plot{f(x) & x_1|0 & x_2|0 @ x = x_min : x_max}
    #else
        'The discriminant is less than zero. There are no real roots.
        #hide
        d = -b/(2*a)
        x_min = d + abs(a)
        x_max = d - abs(a)
        #post
        $Plot{f(x) @ x = x_min : x_max}
    #end if
#end if
Rendered Output:

f ( x )  = a · x2 + b · x + c = 0

Coefficients:

a = 2 , b = 3 , c = -5

Discriminant:

D = b2 − 4 · a · c = 32 − 4 · 2 ·  ( -5 )  = 49

The discriminant is grater or equal to zero. Real roots exist.

q = -0.5 ·  ( b + sign ( b ≡ 0 + b )  ·    D )  = -0.5 ·  ( 3 + sign ( 3 ≡ 0 + 3 )  ·     49 )  = -5

x1 = qa = -52 = -2.5 , x2 = cq = -5-5 = 1

Plot

Cubic Equation

Solves \(x^3 + a x^2 + b x + c = 0\) with the built-in numerical root finder, recovering all real and complex roots from the user-supplied coefficients.

Code:
f(x) = x^3 + a*x^2 + b*x + c'= 0
'Coefficients:
a = ? {-6}','b = ? {11}','c = ? {-6}
#hide
PlotWidth = 360''PlotHeight = 240
#post
#rad
'Solution:
Q = (a^2 - 3*b)/9
R = (2*a^3 - 9*a*b + 27*c)/54
#if R*R < Q*Q*Q
    'Check:'R^2'&lt;'Q^3'
    'There are three real roots.
    θ = acos(R/sqr(Q^3))
    x_1 = -2*sqr(Q)*cos(θ/3) - a/3
    x_2 = -2*sqr(Q)*cos((θ - 2*π)/3) - a/3
    x_3 = -2*sqr(Q)*cos((θ + 2*π)/3) - a/3
    #hide
    d = max(abs(x_3 - x_1)/4; 0.5)
    x_min = min(x_1; x_3) - d
    x_max = max(x_1; x_3) + d
    #post
    $Plot{f(x) & x_1|0 & x_2|0 & x_3|0 @ x = x_min : x_max}
#else
    'Check:'R^2'&ge;'Q^3'
    'There is one real root.
    A = -sign(R)*(abs(R) + sqr(R^2 - Q^3))^(1/3)
    #if A  0
        x_1 = -a/3
    #else
        x_1 = A + Q/A - a/3
    #end if
    #hide
    d = max(abs(a); max(abs(b); abs(c)))
    x_min = x_1 - d
    x_max = x_1 + d
    #post
    $Plot{x|f(x) & x_1|0 @ x = x_min : x_max}
#end if
Rendered Output:

f ( x )  = x3 + a · x2 + b · x + c = 0

Coefficients:

a = -6 , b = 11 , c = -6

Solution:

Q = a2 − 3 · b9 =  ( -6 ) 2 − 3 · 119 = 0.333

R = 2 · a3 − 9 · a · b + 27 · c54 = 2 ·  ( -6 ) 3 − 9 ·  ( -6 )  · 11 + 27 ·  ( -6 ) 54 = 0

Check: R2 = 02 = 0 < Q3 = 0.3333 = 0.037

There are three real roots.

θ = acos(R   Q3) = acos(0    0.3333) = 1.57

x1 = -2 ·    Q · cos(θ3)a3 = -2 ·     0.333 · cos(1.573)-63 = 1

x2 = -2 ·    Q · cos(θ − 2 · π3)a3 = -2 ·     0.333 · cos(1.57 − 2 · 3.143)-63 = 2

x3 = -2 ·    Q · cos(θ + 2 · π3)a3 = -2 ·     0.333 · cos(1.57 + 2 · 3.143)-63 = 3

Plot

Bitwise Operations

Custom multiline functions implementing decimal-to-binary, octal and hexadecimal conversions and the standard bitwise operators.

Code:
'<h5>Decimal to binary:</h5>
dec2bin(x; n) = $sum{mod(floor(x/2^k); 2)*10^k @ k = 0 : n - 1}
#def dec2bin$(x; n$) = dec2bin(x; n$):Dn$'₂
'<h5>Binary to decimal:</h5>
bin2dec(x; n) = $sum{mod(floor(x/10^k); 2)*2^k @ k = 0 : n - 1}
'Helper function -'fl_2(x) = floor(log_2(x))
'<h5>NOT X:</h5>
NOT_b(x) = $sum{2^n*((floor(x/2^n)2 + 1)2) @ n = 0 : fl_2(x)}
'<h5>X AND Y:</h5>
AND_b(x; y) = $sum{2^n*(floor(x/2^n)2)*(floor(y/2^n)2) @ n = 0 : fl_2(x)}
'<h5>X OR Y:</h5>
OR_b(x; y) = $sum{2^n*(
    floor(x/2^n)2 + floor(y/2^n)2 - _
    (floor(x/2^n)2)*(floor(y/2^n)2) _
  ) @ n = 0 : floor(log_2(x))}
'<h5>X XOR Y:</h5>
XOR_b(x; y) = $sum{2^n*((floor(x/2^n)2 + floor(y/2^n)2)2) @ n = 0 : fl_2(x)}
'<h5>Examples</h5>
#nosub
x = 74'₁₀ , 'x_2 = dec2bin$(x; 7)
y = 53'₁₀ , 'y_2 = dec2bin$(y; 7)
#varsub
NOT_b(x)'(0110101 ₂)
AND_b(x; y)'(0000000 ₂)
OR_b(x; y)'(1111111 ₂)
XOR_b(x; y)'(1111111 ₂)
XOR_b(x; x)'(0000000 ₂)
Rendered Output:
Decimal to binary:

dec2bin ( x; n )  = n − 1k= 0mod(floor(x2k); 2) · 10k

Binary to decimal:

bin2dec ( x; n )  = n − 1k= 0mod(floor(x10k); 2) · 2k

Helper function - fl2 ( x )  = floor ( log2 ( x )  ) 

NOT X:

NOTb ( x )  = fl2  ( x ) n= 02n · (floor(x2n) mod 2 + 1) mod 2

X AND Y:

ANDb ( x; y )  = fl2  ( x ) n= 02n · floor(x2n) mod 2 · floor(y2n) mod 2

X OR Y:

ORb ( x; y )  = floor ( log2 ( x )  ) n= 02n · (floor(x2n) mod 2 + floor(y2n) mod 2 − floor(x2n) mod 2 · floor(y2n) mod 2)

X XOR Y:

XORb ( x; y )  = fl2  ( x ) n= 02n · (floor(x2n) mod 2 + floor(y2n) mod 2) mod 2

Examples

x = 74 ₁₀ , x2 = dec2bin  ( x; 7 )  = 1001010

y = 53 ₁₀ , y2 = dec2bin  ( y; 7 )  = 0110101

NOTb  ( x )  = NOTb  ( 74 )  = 53 (0110101 ₂)

ANDb  ( x; y )  = ANDb  ( 74; 53 )  = 0 (0000000 ₂)

ORb  ( x; y )  = ORb  ( 74; 53 )  = 127 (1111111 ₂)

XORb  ( x; y )  = XORb  ( 74; 53 )  = 127 (1111111 ₂)

XORb  ( x; x )  = XORb  ( 74; 74 )  = 0 (0000000 ₂)

Combinatorics

Tabulates permutations and combinations with and without repetition using the built-in \(perm\) and \(comb\) helpers.

Code:
'<h4>1. Permutations</h4> (order of element matters)
'<h5>1.1. Permutations without repetitions </h5>
'Number of permutations of n elements -'P(n) = n!'(factorial function)
'Example -'P(5)
'Number of permutations of k elements from n possible -'P(n; k) = n!/(n - k)!
'Example -'P(5; 2)
'Efficient method for calculation -'P(n; k) = $Product{n - i @ i = 0 : k - 1}
'Example -'P(5; 2)
'<h5>1.2. Permutations with repetitions</h5>
'Number of permutations of k elements from n possible -'P(n; k) = n^k
'Example -'P(5; 2)
'<h4>2. Combinations</h4> (order of element doesn′t matter)
'<h5>2.1. Combinations without repetitions</h5>
'Number of combinations of k elements from n possible -'C(n; k) = n!/((n - k)!*k!)
'(a.k.a. binomial coefficients)
'Example -'C(5; 2)
'Efficient method for calculation -'C(n; k) = $Product{1 + (n - k)/i @ i = 1 : k}
'Example -'C(5; 2)' = 'C(5; 5 - 2)
'<h5>2.1. Combinations with repetitions</h5>
'Number of combinations of k elements from n possible -'C(n; k) = (k + n - 1)!/((n - 1)!*k!)
'Example -'C(5; 2)
'Efficient method for calculation -'C(n; k) = $Product{1 + (n - 1)/i @ i = 1 : k}
'Example -'C(5; 2)
Rendered Output:
1. Permutations
(order of element matters)
1.1. Permutations without repetitions

Number of permutations of n elements - P ( n )  = n! (factorial function)

Example - P  ( 5 )  = 120

Number of permutations of k elements from n possible - P ( n; k )  = n! ( nk ) !

Example - P  ( 5; 2 )  = 20

Efficient method for calculation - P ( n; k )  = k − 1i= 0(ni)

Example - P  ( 5; 2 )  = 20

1.2. Permutations with repetitions

Number of permutations of k elements from n possible - P ( n; k )  = nk

Example - P  ( 5; 2 )  = 25

2. Combinations
(order of element doesn′t matter)
2.1. Combinations without repetitions

Number of combinations of k elements from n possible - C ( n; k )  = n! ( nk ) ! · k!

(a.k.a. binomial coefficients)

Example - C  ( 5; 2 )  = 10

Efficient method for calculation - C ( n; k )  = ki= 1(1 + nki)

Example - C  ( 5; 2 )  = 10 = C  ( 5; 5 − 2 )  = 10

2.1. Combinations with repetitions

Number of combinations of k elements from n possible - C ( n; k )  =  ( k + n − 1 ) ! ( n − 1 ) ! · k!

Example - C  ( 5; 2 )  = 15

Efficient method for calculation - C ( n; k )  = ki= 1(1 + n − 1i)

Example - C  ( 5; 2 )  = 15

Functions of Multiple Parameters

Showcases the variadic helpers \(min\), \(max\), \(gcd\) and \(lcm\), which accept any number of comma- or semicolon-separated arguments.

Code:
"Functions of multiple variables
a = min(2; 3; -1; 5; 7; -4)' - minimum
b = max(2; 3; -1; 5; 7; -4; 9; 6)' - maximum
s = sum(2; 3; -1; 5; 7; -4)' - sum
s_1 = sumsq(2; 3; -1; 5; 7; -4)' - sum of squares
s_2 = srss(2; 3; -1; 5; 7; -4)' - square root of sum of squares
c = average(2; 3; -1; 5; 7; -4)' - average
p = product(2; 3; 2; 5)' - product
m = mean(2; 3; 2; 5)' - geometric mean
f(x) = switch(x < -2; 1; x > 4; x - 2; x > 1; 2; -1)' - selective evaluation
'<!--'PlotWidth = 200','PlotHeight = 120'-->
$Plot{f(x) @ x = -4 : 6}
a(n) = take(n; 2; 3; -1; 5; 7; -4)' - vector function (returns a number with specified index)
b(n) = a(7 - n)' - transpoze
c(n) = a(n)*b(n)' - dot product
'Output vectors to a table
#val
'<!--'n = 0'-->
'<table class="bordered">
'<tr><th>n</th><th>a</th><th>b</th><th>c</th></tr>
#repeat 6
    '<tr><th>'n = n + 1'</th><td>'a(n)'</td><td>'b(n)'</td><td>'c(n)'</td></tr>
#loop
'</table>
#equ
'Matrix
R_1(j) = take(j; 11; 12; 13)' - first row
R_2(j) = take(j; 21; 22; 23)' - second row
R_3(j) = take(j; 31; 32; 33)' - third row
M(i; j) = take(i; R_1(j); R_2(j); R_3(j))' - assemble the whole matrix
M(2; 2)
M(3; 1)
M(3; 3)
'Output a matrix to a table
#val
'<!--'i = 0''j = 0'-->
'<table class="bordered">
'<tr><th></th>
#repeat 3
    '<th>'j = j + 1'</th>
#loop
'</tr>
#repeat 3
    '<tr><th>'i = i + 1'<!--'j = 0'--></th>
    #repeat 3
        '<td><!--'j = j + 1'-->'M(i; j)'</td>
    #loop
    '</tr>
#loop
'</table>
#equ
Rendered Output:
Functions of multiple variables

a = min ( 2; 3; -1; 5; 7; -4 )  = -4 - minimum

b = max ( 2; 3; -1; 5; 7; -4; 9; 6 )  = 9 - maximum

s = sum ( 2; 3; -1; 5; 7; -4 )  = 12 - sum

s1 = sumsq ( 2; 3; -1; 5; 7; -4 )  = 104 - sum of squares

s2 = srss ( 2; 3; -1; 5; 7; -4 )  = 10.2 - square root of sum of squares

c = average ( 2; 3; -1; 5; 7; -4 )  = 2 - average

p = product ( 2; 3; 2; 5 )  = 60 - product

m = mean ( 2; 3; 2; 5 )  = 2.78 - geometric mean

f ( x )  = {if x <  ( -2 ) : 1
else if x > 4: x − 2
else if x > 1: 2
else: -1
- selective evaluation

Plot

a ( n )  = take ( n; 2; 3; -1; 5; 7; -4 )  - vector function (returns a number with specified index)

b ( n )  = a  ( 7 − n )  - transpoze

c ( n )  = a  ( n )  · b  ( n )  - dot product

Output vectors to a table

nabc
12-4-8
23721
3-15-5
45-1-5
57321
6-42-8

Matrix

R1 ( j )  = take ( j; 11; 12; 13 )  - first row

R2 ( j )  = take ( j; 21; 22; 23 )  - second row

R3 ( j )  = take ( j; 31; 32; 33 )  - third row

M ( i; j )  = take ( i; R1  ( j ) ; R2  ( j ) ; R3  ( j )  )  - assemble the whole matrix

M  ( 2; 2 )  = 22

M  ( 3; 1 )  = 31

M  ( 3; 3 )  = 33

Output a matrix to a table

123
1111213
2212223
3313233

Himmelblau Function

Plots the four-minimum Himmelblau test surface \(f(x, y) = (x^2 + y - 11)^2 + (x + y^2 - 7)^2\), a standard optimisation benchmark.

Code:
"Himmelblau's function
f(x; y) = (x^2 + y - 11)^2 + (x + y^2 - 7)^2
'<!--'PlotHeight = 300''PlotWidth = 300'-->
$Map{min(f(x; y); 500) @ x = -5 : 5 & y = -5 : 5}
z_inf = $inf{$inf{f(x; y) @ x = -5 : 0} @ y = -5 : 0}
x_inf','y_inf
z_inf = $inf{$inf{f(x; y) @ x = -5 : 0} @ y = 0 : 5}
x_inf','y_inf
z_inf = $inf{$inf{f(x; y) @ x = 0 : 5} @ y = 0 : 5}
x_inf','y_inf
z_inf = $inf{$inf{f(x; y) @ x = 0 : 5} @ y = -5 : 0}
x_inf','y_inf
Rendered Output:
Himmelblau's function

f ( x; y )  =  ( x2 + y − 11 ) 2 +  ( x + y2 − 7 ) 2

Plot

zinf = $inf{$inf{f  ( x; y ) ; x ∈ [-5; 0]}; y ∈ [-5; 0]} = 5.49×10-27

xinf = -3.78 , yinf = -3.28

zinf = $inf{$inf{f  ( x; y ) ; x ∈ [-5; 0]}; y ∈ [0; 5]} = 2.43×10-27

xinf = -2.81 , yinf = 3.13

zinf = $inf{$inf{f  ( x; y ) ; x ∈ [0; 5]}; y ∈ [0; 5]} = 1.4×10-27

xinf = 3 , yinf = 2

zinf = $inf{$inf{f  ( x; y ) ; x ∈ [0; 5]}; y ∈ [-5; 0]} = 4.83×10-28

xinf = 3.58 , yinf = -1.85

Spotted an error? Edit these examples.