Openbugs too many constants error - winbugs

I'm trying to run the code below in which I used a zero trick for my log-likelihood expression(phi is my log-likelihood):
model{
for (l in 1:k) {
d.1[l] ~ dbern(p.1)
d.2[l] ~ dbern(p.2)
d.3[l] ~ dbern(p.3)
d.4[l] ~ dbern(p.4)
}
for (l in 1:k) {
zeros[l] <- 0
zeros[l] ~ dpois(phi[l])
u.1[l] <- pow((1 - p.1), (1 - d.1[l]))
u.2[l] <- pow((1 - p.2), (1 - d.2[l]))
u.3[l] <- pow((1 - p.3), (1 - d.3[l]))
u.4[l] <- pow((1 - p.4), (1 - d.4[l]))
f.1[l] <- pow(p.1, d.1[l]) * pow((1 - p.1), (1 - d.1[l]))
f.2[l] <- pow(p.2, d.2[l]) * pow((1 - p.2), (1 - d.2[l]))
f.3[l] <- pow(p.3, d.3[l]) * pow((1 - p.3), (1 - d.3[l]))
f.4[l] <- pow(p.4, d.4[l]) * pow((1 - p.4), (1 - d.4[l]))
a[l] <- pow((pow(u.3[l], -theta.1) + pow(u.4[l], -theta.1) -
1), (theta.0/theta.1 - 2)) * (-1) * ((theta.0/theta.1) *
(-1) + pow((theta.0/theta.1), 2))
b[l] <- pow((pow(u.1[l], -theta.2) + pow(u.2[l], -theta.2) -
1), (theta.0/theta.2 - 2)) * (-1) * ((theta.0/theta.2) *
(-1) + pow((theta.0/theta.2), 2))
c[l] <- pow(pow((pow(u.3[l], -theta.1) + pow(u.4[l],
-theta.1) - 1), -(1/theta.1)), -theta.0) + pow(pow((pow(u.2[l],
-theta.2) + pow(u.1[l], -theta.2) - 1), -(1/theta.2)),
-theta.0) - 1
d[l] <- pow((pow(u.3[l], -theta.1) + pow(u.4[l], -theta.1) -
1), (theta.0/theta.1 * 2 - 2)) * pow(theta.0/theta.1,
2)
e[l] <- pow((pow(u.1[l], -theta.2) + pow(u.2[l], -theta.2) -
1), (theta.0/theta.2 * 2 - 2)) * pow(theta.0/theta.2,
2)
phi[l] <- log((a[l] * b[l]) * (1 + theta.0)/pow(c[l],
-(2 + 1/theta.0)) + (d[l] * b[l] + a[l] * e[l]) *
((1 + theta.0) * (1 + 2 * theta.0))/pow(theta.0,
3) * pow(c[l], -(3 + 1/theta.0)) + (d[l] * e[l]) *
((1 + theta.0) * (1 + 2 * theta.0) * (1 + 3 * theta.0))/pow(theta.0,
4) * pow(c[l], -(4 + 1/theta.0))) + log(pow(theta.1,
2) * pow((u.3[l] * u.4[l]), -(1 + theta.1)) * pow(theta.2,
2) * pow((u.1[l] * u.2[l]), -(1 + theta.2)))
}
p.1 ~ dunif(0 , 1)
p.2 ~ dunif(0 , 1)
p.3 ~ dunif(0 , 1)
p.4 ~ dunif(0 , 1)
theta.0 ~ dunif(0 , 2)
theta.1 ~ dunif(0 , 2)
theta.2 ~ dunif(0 , 2)
}
with data:
k <- 16
d.1 <- c(0 , 0, 0 , 0 , 1 , 0 , 1 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 1 , 0)
d.2 <- c(0 , 1, 0 , 0 , 0 , 0 , 1 , 0 , 1 , 1 , 0 , 1 , 0 , 0 , 1 , 0)
d.3 <- c(1 , 0, 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 , 0 , 0)
d.4 <- c(0 , 1, 0 , 0 , 0 , 0 , 1 , 0 , 1 , 0 , 0 , 0 , 0 , 1 , 0 , 0)
The model is syntactically correct and the data is loaded but I got the following error:
logical expression contains too many constants
Can anyone please help me solve that problem?

BUGS does not like expressions with too many constants. You need to introduce a sub-step to break them up. See point (c) under 'Some Error Messages' in this page:
http://mathstat.helsinki.fi/openbugs/Manuals/TipsTroubleshooting.html

Related

how to solve subscript out of range in qbasic

I am in big problem in solving a code essential for me, and I need the solution as soon as possible
in fact, I have little knowledge of programming in basic
I have a problem with this code.
I have an equation, and I use this code to solve this equation
when I run the program
this error appears
subscript out of range
is there any solution to this problem
0 Print "******** impact *******"
20 Print "____________"
30 Print "this programs is used to solve impact integral"
40 Print "equation of simply supported slab to "
50 Print "optain the following"
60 Print " (1) force _time history"
70 Print " (2) central deflection - time history"
80 Print "-----------------"
90 Print "input data:"
100 Print " (1) FUNDAMENTAL NATURAL FREQUANCY (RAD/SEC)--- W1"
110 Print " (2) STRIKER MASS (KG.) ----- Mst"
120 Print " (3) MASS OF SLAB (KG)---- Ms"
130 Print " (4) hertz constant (n/m^1.5)----k"
140 Print " (5) STRICKER VELOCITY (M/S)----Vo"
150 Print " (6) NUMBER OF MODES----N"
160 Print "_________"
170 Input " W11, MST, MS, K, VO, N", W11, MST, MS, K, VO, N
180 Print "W1="; W11; "RAD/SEC"
190 Print "MST="; MST; "KG"
200 Print "MS="; MS; "KG"
210
220 Print " K = "; K; "N/M^1.5"
230 Print STANDARD
240 Print "VO="; VO; "M/S"
241 Print " N = "; N
250 Print
260 Print
270 K1 = K
280 V = VO
290 W1 = W11 / 2
300 TINF = 2.94 * (MST / (.8 * K1 * V ^ .5)) ^ .4 * 1000
310 DT = TINF / 10
320 M = 20
330 DT = PROUND(DT, 0)
340 DT = DT / 1000
350 M = 20
360 Option Base 1
370 Dim W(11, 11), Z(11, 11), F(30), D(30), BM(30), SH(30), A(30), T(30), DF(30), S(11, 11, 30), C(11, 11, 30)
380 ReDim W(N, N), Z(N, N)
390 For I = 1 To N Step 2
400 For K = 1 To N Step 2
410 W(K, I) = W1 * (I ^ 2 + K ^ 2)
420 Z(K, I) = W(K, I) * DT
430 Next K
440 Next I
450 ReDim F(M), D(M), A(M), T(M), DF(M), BM(M), SH(M), S(N, N, M), C(N, N, M)
460 F(1) = D(1) = A(1) = T(1) = 0
470 For I = 1 To N Step 2
480 For K = 1 To N Step 2
490 S(K, I, 1) = C(K, I, 1) = 0
500 Next K
510 Next I
520 B1 = 0
530 For I = 1 To N Step 2
540 For K = 1 To N Step 2
550 B1 = B1 + (1 - Sin(Z(K, I)) / Z(K, I)) / W(K, I) ^ 2
560 Next K
570 Next I
580 B = -DT ^ 2 / (6 * MST) - 4 * B1 / MS
590 VE = 0
600 For I = 2 To M
610 T(I) = (I - 1) * DT
620 GM = 0
630 If VE = 1 Then 970
640 For J = 2 To I
650 GM = GM + F(J - 1)
660 Next J
670 AA = 0
680 For J = 1 To N Step 2
690 For K = 1 To N Step 2
700 FF = F(I - 1) * (Sin(Z(K, J)) / Z(K, J) - Cos(Z(K, J))) / W(K, J)
710 AA = AA + 4 * (Cos(Z(K, J)) * S(K, J, I - 1) + Sin(Z(K, J)) * C(K, J, I - 1) + FF) / (MS * W(K, J))
720 Next K
730 Next J
740 A(I - 1) = V * (I - 1) * DT - (D(I - 1) + DT ^ 2 * (GM - F(I - 1) / 6)) / MST - AA
750 F = F(I - 1)
760 If A(I - 1) + B * F < 0 Then 840
770 F1 = (A(I - 1) + B * F) ^ 1.5 * K1
780 X = Abs(F1 - F)
790 If X < 10 Then 820
800 F = F1
810 GoTo 770
820 F(I) = F1
830 GoTo 850
840 F(I) = 0
850 D(I) = D(I - 1) + DT ^ 2 * (GM + (F(I) - F(I - 1)) / 6)
860 For J = 1 To N Step 2
870 For K = 1 To N Step 2
880 S(K, J, I) = Cos(Z(K, J)) * S(K, J, I - 1) + Sin(Z(K, J)) * C(K, J, I - 1) + (1 - Sin(Z(K, J)) / Z(K, J)) * (F(I) - F(I - 1)) / W(K, J) + (1 - Cos(Z(K, J))) * F(I - 1) / W(K, J)
890 C(K, J, I) = Cos(Z(K, J)) * C(K, J, I - 1) - Sin(Z(K, J)) * S(K, J, I - 1) + (1 - Cos(Z(K, J))) / Z(K, J) * (F(I) - F(I - 1)) / W(K, J) + Sin(Z(K, J)) * F(I - 1) / W(K, J)
900 Next K
910 Next J
920 DF = 0
930 For J = 1 To N Step 2
940 For K = 1 To N Step 2
950 DF = DF + 4 * S(K, J, I) / W(K, J) / MS
960 Next K
970 Next J
980 DF(I) = DF
990 If F(I) = 0 Then 1010
1000 Next I
1010 Print "----------------------------------------------------------"
1020 Print "{TIME (MS)},{FORCE (KN)},{DEFLECTION(MM)}"
1030 Print "----------------------------------------------------------"
1040 II = I
1050 For O = 1 To II
1060
1070 Print Tab(1); ":"; Tab(5); T(0) * 1000; Tab(18); ":"; Tab(22); F(O) / 1000; Tab(34); ":"; Tab(42); DF(O) * 1000; Tab(56); ":"
1080 Print "-----------------------------------------------------------"
1090 Next O
1100 End

How to create a matrix in Matlab with every entry being the output of a bivariate function

I want to create a 4 x 4 matrix with each entry representing f(x,y) where both x and y take values 0, 1, 2 and 3. So the first entry would be f(0,0), all the way to f(3,3).
The function f(x,y) is:
3 * cos(0*x + 0*y) + 2 * cos(0*x + 1*y) + 3 * cos(0*x + 2*y) + 8 * cos(0*x + 3*y)
+ 3 * cos(1*x + 0*y) + 25 * cos(1*x + 1*y) + 3 * cos(1*x + 2*y)
+ 8 * cos(1*x + 3*y)
+ 3 * cos(2*x + 0*y) + 25 * cos(2*x + 1*y) + 3 * cos(2*x + 2*y)
+ 8 * cos(2*x + 3*y)
+ 3 * cos(3*x + 0*y) + 25 * cos(3*x + 1*y) + 3 * cos(3*x + 2*y)
- 90 * cos(3*x + 3*y)
I haven't used Matlab much, and it's been a while. I have tried turning f(x,y) into a #f(x,y) function; using the .* operator; meshing x and y, etc. All of it without success...
Not sure, what you've tried exactly, but using meshgrid is the correct idea.
% Function defintion (abbreviated)
f = #(x, y) 3 * cos(0*x + 0*y) + 2 * cos(0*x + 1*y) + 3 * cos(0*x + 2*y)
% Set up x and y values.
x = 0:3
y = 0:3
% Generate grid.
[X, Y] = meshgrid(x, y);
% Rseult matrix.
res = f(X, Y)
Generated output:
f =
#(x, y) 3 * cos (0 * x + 0 * y) + 2 * cos (0 * x + 1 * y) + 3 * cos (0 * x + 2 * y)
x =
0 1 2 3
y =
0 1 2 3
res =
8.00000 8.00000 8.00000 8.00000
2.83216 2.83216 2.83216 2.83216
0.20678 0.20678 0.20678 0.20678
3.90053 3.90053 3.90053 3.90053

How to use "field" with rationals?

Can anybody tell me why the tactic "field" does not work when I try to prove the following goal involving rationals?
nat_to_Q 3 * nat_to_Q n * nat_to_Q n + nat_to_Q 3 * nat_to_Q n +
nat_to_Q 3 * nat_to_Q n + nat_to_Q 3 + nat_to_Q 3 * nat_to_Q n +
nat_to_Q 3 + nat_to_Q n * nat_to_Q n * nat_to_Q n + nat_to_Q n * nat_to_Q n +
nat_to_Q n * nat_to_Q n * nat_to_Q 2 + nat_to_Q n * nat_to_Q 2 ==
nat_to_Q 3 * nat_to_Q n * nat_to_Q n * nat_to_Q n +
nat_to_Q 6 * nat_to_Q n * nat_to_Q n + nat_to_Q 11 * nat_to_Q n +
nat_to_Q 6
Note: n is nat and nat_to_Q is (Z.of_nat n # Pos.of_nat 1).
Thanks a lot,
Marcus.
Let's remove the coercions to make this easier to read:
3 * n * n + 3 * n +
3 * n + 3 * n +
3 + n * n * n + n * n +
n * n * 2 + n * 2 ==
3 * n * n * n +
6 * n * n + 11 * n + 6
Now we can see the issue: the goal does not hold. The 3rd order coefficient on the left-hand side is 1, but on the right-hand side it is 3.

Printing a star pattern in swift using stride function

Right now i am trying to print following star pattern using swift 4.
1
12
123
1234
For this i have taken below approach.
for i in stride(from: 1, to: 5, by: 1) {
// prints from 1 to 4
for j in stride(from: 1, to: i, by: 1) {
print(j, separator: "", terminator: "")
}
print("*\n")
}
But right now this will print below output.
*
1*
12*
123*
Any way to fix this issue ?
Here are some logical pattern in swift 4 ,
0
01
012
0123
01234
for i in 0...4{
for j in stride(from: 4, to: i, by: -1){
print( terminator : " ")
}
for k in 0...i{
print(k,terminator : "")
}
print(" ")
}
54321
5432
543
54
5
for i in stride(from: 0, to: 5, by: 1){
for j in stride(from: 5, to: i, by: -1){
print(j , terminator : "")
}
print(" ")
}
5
5 4
5 4 3
5 4 3 2
5 4 3 2 1
for i in stride(from: 5, to: 0, by: -1)
{
for j in stride(from: 5, to: i-1, by: -1){
print(j , terminator : "")
}
print(" ")
}
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
for i in stride(from: 5, to: 0, by: -1)
{
for j in 1...i{
print(j, terminator : "")
}
print(" ")
}
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
var value = 0
for i in 1...5 {
for j in 1...i{
value = value + 1
print(value,terminator : "")
}
print(" ")
}
1
2 1
3 2 1
4 3 2 1
5 4 3 2 1
for i in 1...5{
for j in 1...i{
print(i+1 - j , terminator : "")
}
print(" ")
}
for i in 1...5{
for j in stride(from: i, to: 0, by: -1){
print(j, terminator : "")
}
print(" ")
}
1
2 7
3 8 13
4 9 14 19
5 10 15 20 25
var value = 0
for i in 1...5{
for j in 1...i{
if j != 1{
value = value + 5
}
else{
value = i
}
print(value , terminator : " ")
}
print(" ")
}
for i in 1...5{
var temp = i
for j in 0...i{
print(temp , terminator : " ")
temp = temp + 5
}
print(" ")
}
1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1
var value = 1
for i in 1...5{
for j in 1...i{
print(j,terminator : "")
}
for k in 1..<i{
print(i-k,terminator : "")
}
print(" ")
}
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
for i in stride(from: 5, to: 0, by: -1){
for k in stride(from: 5, to: i, by: -1) {
print(terminator : " ")
}
for j in stride(from: 1, to: i+1, by: 1){
print("*",terminator : " ")
}
print(" ")
}
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
for i in 1...5{
for j in stride(from: 5, to: i, by: -1){
print(i,terminator : "")
}
var temp = 1
for k in 1...i{
print(temp,terminator : "")
temp = temp * (i - k) / (k);
}
print(" ")
}
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
for i in 1...5{
for k in stride(from: 5, to: i, by: -1) {
print(terminator : " ")
}
for j in 1...i{
print(j,terminator : " ")
}
print(" ")
}
for i in stride(from: 5, to: 0, by: -1){
for k in stride(from: 5, to: i-1, by: -1) {
print(terminator : " ")
}
for j in stride(from: 1, to: i, by: 1){
print(j,terminator : " ")
}
print(" ")
}
12345
2345
345
45
5
5
45
345
2345
12345
for i in 1...5{
for j in stride(from: i, to: 6, by: 1){
print(j , terminator : "")
}
print(" ")
}
for i in stride(from: 5, to: 0, by: -1)
{
for j in stride(from: i, to: 6, by: 1){
print(j,terminator : "")
}
print(" ")
}
1 2 3 4 5
2 3 4 5
3 4 5
4 5
5
for i in 1...5{
for k in 0...i{
print(terminator : " ")
}
for j in stride(from: i, to: 6, by: 1){
print(j , terminator : " ")
}
print(" ")
}
12345
2345
345
45
5
5
45
345
2345
12345
for i in 1...5{
for k in 1...i{
print(terminator : " ")
}
for j in stride(from: i, to: 6, by: 1){
print(j , terminator : "")
}
print(" ")
}
for i in stride(from: 5, to: 0, by: -1)
{
for k in 1...i{
print(terminator : " ")
}
for j in stride(from: i, to: 6, by: 1){
print(j,terminator : "")
}
print(" ")
}
1 2 3 4 5
2 3 4 5
3 4 5
4 5
5
5
4 5
3 4 5
2 3 4 5
1 2 3 4 5
for i in 1...5{
for k in 0...i{
print(terminator : " ")
}
for j in stride(from: i, to: 6, by: 1){
print(j , terminator : " ")
}
print(" ")
}
for i in stride(from: 6, to: 1, by: -1){
for k in 1...i{
print(terminator : " ")
}
for j in stride(from: i-1, to: 6, by: 1){
print(j , terminator : " ")
}
print(" ")
}
1
1 0
1 0 1
1 0 1 0
1 0 1 0 1
for i in 0...4{
for j in 0...i{
if j % 2 == 0{
print(1,terminator : " ")
}
else{
print(0,terminator : " ")
}
}
print(" ")
}
1 0 0 0 0
0 2 0 0 0
0 0 3 0 0
0 0 0 4 0
0 0 0 0 5
for i in 1...5{
for j in 1...5{
if j == i{
print(j,terminator : " ")
}
else{
print(0,terminator : " ")
}
}
print(" ")
}
If you want
1
12
123
1234
for i in 1..<5 { // or for i in stride(from: 1, to: 5, by: 1) {
for j in 1...i { // for j in stride(from: 1, through: i, by: 1) {
print(j, terminator: "")
}
print("")
}
If you want
*
**
***
****
for i in 1..<5 {
for _ in 1...i {
print("*", terminator: "")
}
print("")
}
1) Pyramid
*
* *
* * *
* * * *
* * * * *
LINK :-
https://replit.com/#vishakhaagarwa1/DarkorchidSmoothPayware#main.swift
for i in 1...5
{
for _ in stride(from: 5, to: i, by: -1)
{
print(terminator : " ")
}
for _ in 1...i
{
print("*",terminator : " ")
}
print(" ")
}
2) Right Triangle
*
* *
* * *
* * * *
* * * * *
LINK :-
https://replit.com/#vishakhaagarwa1/StingyOldApplicationstack#main.swift
var i = 1
var j = 1
for i in i...5
{
for _ in j...i
{
print("*",terminator : "")
}
print("")
}
3) Mirrored Right Triangle
*
* *
* * *
* * * *
* * * * *
LINK :-
https://replit.com/#vishakhaagarwa1/CookedParallelRobodoc#main.swift
for i in 1...5
{
for _ in stride (from: 5, to: i, by: -1)
{
print(" ",terminator : "")
}
for _ in 1...i
{
print("*",terminator : "")
}
print("")
}
4) Diamond
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
LINK :-
https://replit.com/#vishakhaagarwa1/WarmEmptyDegrees#main.swift
for i in 1...10
{
if i % 2 != 0
{
for _ in stride(from: 10, to: i, by: -1)
{
print(terminator : " ")
}
for _ in 1...i
{
print("*",terminator : " ")
}
print(" ")
}
}
for i in stride(from: 10, to: 1, by: -1)
{
if i % 2 != 0
{
for _ in stride(from: 10, to: i-1, by: -1)
{
print(terminator : " ")
}
for _ in stride(from: 2, to: i, by: 1)
{
print("*",terminator : " ")
}
print(" ")
}
}
5) Downward Triangle
* * * * *
* * * *
* * *
* *
*
LINK :-
https://replit.com/#vishakhaagarwa1/FearlessIndolentByte#main.swift
for i in 1...5
{
for _ in stride(from: i, to: 6, by: 1)
{
print("*",terminator : "")
}
print("")
}
6) Right Pascal’s Triangle
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
LINK :-
https://replit.com/#vishakhaagarwa1/IndianredNervousPrinter#main.swift
for i in 1...5
{
for _ in 1...i
{
print("*",terminator : "")
}
print("")
}
for i in 1...4
{
for _ in stride (from: 5, to: i, by: -1)
{
print("*",terminator : "")
}
print("")
}
7) Sandglass Pattern
* * * * *
* * * *
* * *
* *
*
*
* *
* * *
* * * *
* * * * *
LINK :-
https://replit.com/#vishakhaagarwa1/TangibleDeeppinkWatch#main.swift
for i in 1...5
{
for _ in 1...i
{
print(terminator: " ")
}
for _ in stride(from: i, to: 6, by: 1)
{
print("*",terminator : " ")
}
print("")
}
for i in 1...5
{
for _ in stride(from: 6, to: i, by: -1)
{
print(terminator : " ")
}
for _ in 1...i
{
print("*",terminator : " ")
}
print(" ")
}
for i in 0...5{
for _ in 0...i{
print("*", terminator: "")
}
print() }
for i in stride(from: 5, to: 0, by: -1){
for _ in stride(from: i, to: 0, by: -1){
print("*",terminator: "")
}
print() }
output :
*
**
***
****
*****
******
*****
****
***
**
*
var q : String = ""
repeat
{
print("***** Numerical Patterns *****")
print("1 for Number Right Triangle.. ")
print("2 for Sequential Number Right Triangle.. ")
print("3 for Pascal Number Triangle.. ")
print("Enter Your Choice :: ")
guard let index = Int(readLine()!), index > 0 && index < 4 else
{
print(" !!!! WArning :: Wrong Selection.......")
print("\n")
continue
}
var n: Int
print("Enter N Number For Pattern :: ")
let num = readLine()
if let t2 = Int(num!)
{
n = t2
}
else
{
print("wrong input ! taking default [5]")
n = 5
}
switch index {
case 1 :
for i in 1...n
{
for j in 1...i
{
print(j,terminator: " ")
}
print("")
}
case 2 :
var j = 0
for i in 1...n
{
for _ in 1...i
{
j = j+1
print(j,terminator: " ")
}
print("")
}
case 3 :
for i in 1...n
{
//print("i==",i)
for _ in stride(from: n, to: i, by: -1)
{
//print("k==",k)
print(terminator : " ")
}
var no = 1
for j in 1...i
{
//print("j==",j)
print(no,terminator : " ")
no = no * (i - j) / (j)
}
print(" ")
}
default :
print( "Wrong Input............")
}
print("\n")
print("Do you want to continues [PRESS Y/y] OR CLOSE [PRESS N/n] :: ")
q = (readLine()!)
print("\n")
}while(q == "y" || q == "Y")
func starprint(_ int : Int)
{
for i in 1...int
{
print(String.init(repeating: " ", count: int-i)+String.init(repeating: "*", count: i))
}
}
starprint(6)
for i in stride(from: 1, to: 6, by: 1) {
// prints from 1 to 4
for j in stride(from: 1, to: i, by: 1) {
print(j, separator: "", terminator: "")
}
print("")
}
You can use for loop with below sequencing.
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
func printLadderNumber(_ n: Int) {
for i in 1...n {
for j in 1...i {
print(j, terminator: "")
}
print("", terminator: "\n")// Helps for printing new line
}
}
printLadderNumber(4)
You can find various Star Patterns Question here
https://janeshswift.com/ios/dsa/array/how-to-print-patterns-in-swift/
1. Pyramid
1
1 2
1 2 3
1 2 3 4
for i in 1...4{
for _ in 1...5-i{
print(terminator:" ")
}
for j in 1...i{
print(j,terminator:" ")
}
print("")
}
2. Inverted Pyramid
1 2 3 4
1 2 3
1 2
1
for i in 1...4{
for _ in 1...i{
print(terminator:" ")
}
for j in 1...5-i{
print(j,terminator:" ")
}
print("")
}

Understanding recursive function with implicit return statement

Below recursive method sums the integer values between a range
def sumInts(a: Int, b: Int): Int = {
if(a > b) 0
else {
println(a +"," + b)
a + sumInts(a + 1 , b)
}
}
So sumInts(2 , 5) returns 14
I'm confused about how the recursive call to sumInts sums the integer range. Can explain textually how this method works ?
How does sumInts return the incremented value ?? Perhaps I am missing something fundamental to recursion here
It calculates the sum of values in the range [a, b] by first calculating the sum of the range [a+1, b] (by recursively calling sumInts(a + 1 , b)) then adding a to it.
[Update] In Scala, the return statement is optional; functions return the value of the last expression evaluated. Thus the above function body is equivalent to
if(a > b) return 0
else {
println(a +"," + b)
return a + sumInts(a + 1 , b)
}
[/Update]
Which for the range [2, 5] it would do the following (I removed the println call for the sake of simplicity, and added brackets to mark recursive calls):
if(2 > 5) 0 else 2 + sumInts(2 + 1, 5) which, the condition being false, evaluates to
2 + sumInts(3, 5)
2 + (if(3 > 5) 0 else 3 + sumInts(3 + 1, 5)) which evaluates to
2 + (3 + sumInts(4, 5))
2 + (3 + (if(4 > 5) 0 else 4 + sumInts(4 + 1, 5))) which evaluates to
2 + (3 + (4 + sumInts(5, 5)))
2 + (3 + (4 + (if(5 > 5) 0 else 5 + sumInts(5 + 1, 5)))) which evaluates to
2 + (3 + (4 + (5 + sumInts(6, 5))))
2 + (3 + (4 + (5 + (if(6 > 5) 0 else 6 + sumInts(6 + 1, 5))))) which, the condition being true, evaluates to
2 + (3 + (4 + (5 + (0))))