How to get two random effects crossed with one nested in the other in nlme? - logistic-regression

My nonlinear mixed-effects model regresses body mass (bm) on age. I would like consider that brood is nested within year, but as a brood can only occur in one of the seven years that are in the dataset, the random effects of year and brood should be crossed.
In Pinheiro & Bates (2000): ‘Mixed-Effects Models in S and S-plus’ (http://link.springer.com/book/10.1007%2Fb98882), there is one example with crossed random effects: A full factorial model of a cell culture bioassay is used to represent the fixed effects, and three random effects are used to account for block, row, and column effects, with the last two random effects nested within block, but crossed with each other. This is implemented followingly (p. 163 - 165):
options( contrasts = c("contr.treatment", "contr.poly") )
fm1Assay <- lme( logDens ~ sample * dilut, Assay,
random = pdBlocked(list(pdIdent(~ 1), pdIdent(~ sample - 1), pdIdent(~ dilut - 1))) )
Their example is a linear mixed model, but the syntax should work also for nonlinear mixed models. Therefore, I tried the following for my model (I do not know however, why the nested effects have to have -1 after the covariate):
fmbm1 <- nlme(bm ~ SSlogis(age, Asym, xmid, scal), data = bmgrp,
fixed = Asym + xmid + scal ~ 1,
random = pdBlocked(list(pdIdent(Asym + xmid + scal ~ 1),
pdIdent(Asym + xmid + scal ~ brood - 1))),
start = startvalues, na.action = na.omit)
But this syntax seems to be wrong, as it makes R freeze (there isn't even an error message, R just does not respond anymore so that I need to force quitting R).
I would be happy about tips how to realise the nested and crossed structure in nlme.
Here is the structure of my data and below that the previous R code:
> str(bmgrp)
Classes ‘nfnGroupedData’, ‘nfGroupedData’, ‘groupedData’ and 'data.frame': 428 obs. of 7 variables:
$ pop : Factor w/ 2 levels "NL","RUS": 1 2 2 2 2 2 1 2 2 2 ...
$ year : Ord.factor w/ 7 levels "2013"<"2012"<..: 5 1 1 7 1 1 2 1 1 1 ...
$ sex : Factor w/ 2 levels "f","m": 2 1 1 2 1 2 1 2 1 2 ...
$ age : int 0 0 7 8 10 10 10 12 12 12 ...
$ bm : int 80 85 137 165 225 227 132 215 183 195 ...
$ relhatch: num 4.06 4.22 4.22 4.29 1.22 ...
$ brood : Factor w/ 270 levels "","NL2004N1040054",..: 61 189 189 205 190 190 27 187 187 187 ...
bmdata <- read.table(header = TRUE, stringsAsFactors = TRUE, text = "
ID pop year sex age bm relhatch brood
1 NL 2015 m 0 80 4.06293706 NL2015E278
2 RUS 2013 f 0 85 4.22000000 RUS2013N350
7 RUS 2013 f 7 137 4.21888412 RUS2013N350
9 RUS 2014 m 8 165 4.28608247 RUS2014N1372
11 RUS 2013 f 10 225 1.21888412 RUS2013N396
12 RUS 2013 m 10 227 1.21888412 RUS2013N396
13 NL 2012 f 10 132 0.86649874 NL2012E262
17 RUS 2013 m 12 215 -0.78111588 RUS2013N120
18 RUS 2013 f 12 183 -0.78111588 RUS2013N120
19 RUS 2013 m 12 195 -0.78111588 RUS2013N120
21 RUS 2014 m 13 325 -0.71391753 RUS2014N235
22 RUS 2014 m 13 280 -0.71391753 RUS2014N235
27 RUS 2013 m 16 498 -4.78111588 RUS2013N60
28 RUS 2005 m 17 870 3.20000000 RUS2005N1030701
29 NL 2012 m 18 324 -11.13350126 NL2012E45
32 RUS 2003 f 19 880 1.20000000 RUS2003N1030266
33 RUS 2005 f 19 750 6.10000000 RUS2005N1051002
35 RUS 2004 f 20 670 3.20000000 RUS2004N1030477
36 RUS 2004 m 20 790 3.20000000 RUS2004N1030491
37 RUS 2004 m 20 940 3.20000000 RUS2004N1030476
39 RUS 2004 f 20 900 3.20000000 RUS2004N1030521
40 RUS 2004 f 20 740 3.20000000 RUS2004N1030473
41 RUS 2003 m 21 980 -0.80000000 RUS2003N1030429
42 NL 2012 f 21 260 -2.13350126 NL2012Wun23
44 RUS 2014 m 21 620 -3.71391753 RUS2014N248
45 RUS 2003 m 21 1080 -0.80000000 RUS2003N1030045
46 NL 2012 m 21 237 -0.13350126 NL2012E265
47 RUS 2004 m 21 572 2.80000000 RUS2004N1040648
48 RUS 2004 m 21 940 3.20000000 RUS2004N1030482
49 RUS 2004 m 21 450 4.20000000 RUS2004N1030645
53 RUS 2014 f 22 660 -0.71391753 RUS2014N866
54 RUS 2003 f 22 890 -1.80000000 RUS2003N1030415
55 RUS 2005 f 22 684 0.80000000 RUS2005N1040060
56 RUS 2005 f 22 652 0.80000000 RUS2005N1040060
57 RUS 2003 f 22 776 -1.80000000 RUS2003N1030418
58 RUS 2004 m 22 672 1.80000000 RUS2004N1042138
59 RUS 2004 f 22 622 1.80000000 RUS2004N1042138
61 RUS 2004 m 22 472 3.30000000 RUS2004N1042092
62 RUS 2004 f 22 438 3.30000000 RUS2004N1042092
63 NL 2012 m 22 222 7.86649874 NL2012W213
64 RUS 2003 m 22 1410 -1.80000000 RUS2003N1030417
65 RUS 2003 f 22 1010 -1.80000000 RUS2003N1030089
66 RUS 2004 f 22 612 1.20000000 RUS2004N1030470
67 RUS 2004 m 22 598 1.80000000 RUS2004N1042139
68 RUS 2004 f 22 316 1.30000000 RUS2004N1042114
69 RUS 2004 m 22 420 3.20000000 RUS2004N1030474
70 RUS 2004 m 22 370 3.30000000 RUS2004N1042095
76 RUS 2004 f 23 1080 0.20000000 RUS2004N1030467
77 RUS 2003 f 23 854 -2.80000000 RUS2003N1030196
78 RUS 2003 f 23 897 -1.80000000 RUS2003N1030402
79 RUS 2003 m 23 1210 -1.80000000 RUS2003N1030416
80 RUS 2003 f 23 1180 -1.80000000 RUS2003N1030416
81 RUS 2004 m 23 990 0.20000000 RUS2004N1030446
82 RUS 2004 f 23 990 0.20000000 RUS2004N1030446
83 RUS 2004 f 23 1110 0.20000000 RUS2004N1030461
84 RUS 2014 m 23 710 -5.71391753 RUS2014N160
85 RUS 2014 f 23 695 -1.71391753 RUS2014N600
86 RUS 2003 m 23 1230 -1.80000000 RUS2003N1030404
87 RUS 2004 m 23 870 0.20000000 RUS2004N1030465
88 RUS 2004 f 23 500 0.80000000 RUS2004N1040578
89 RUS 2004 m 23 598 0.80000000 RUS2004N1040288
90 RUS 2004 f 23 480 2.80000000 RUS2004N1040685
91 RUS 2004 m 23 444 2.80000000 RUS2004N1040688
92 RUS 2005 f 23 820 -0.80000000 RUS2005N1030706
99 RUS 2004 m 24 1120 -0.80000000 RUS2004N1030467
100 RUS 2004 m 24 612 -0.20000000 RUS2004N1040474
101 RUS 2004 m 24 642 -0.20000000 RUS2004N1040474
102 RUS 2004 m 24 634 -0.20000000 RUS2004N1040474
103 RUS 2014 f 24 740 2.28608247 RUS2014N49
104 RUS 2014 f 24 840 2.28608247 RUS2014N49
105 RUS 2014 m 24 900 2.28608247 RUS2014N49
106 RUS 2005 m 24 972 0.80000000 RUS2005N1040106
107 RUS 2005 f 24 820 0.80000000 RUS2005N1040106
108 RUS 2005 f 24 910 0.80000000 RUS2005N1040106
109 RUS 2014 m 24 980 2.28608247 RUS2014N759
110 RUS 2014 f 24 775 2.28608247 RUS2014N759
111 RUS 2004 f 24 870 0.20000000 RUS2004N1030457
112 RUS 2004 f 24 516 0.80000000 RUS2004N1040528
113 RUS 2004 f 24 672 1.80000000 RUS2004N1040828
114 RUS 2004 f 24 604 1.80000000 RUS2004N1040828
115 RUS 2004 m 24 790 3.20000000 RUS2004N1030491
116 RUS 2004 m 24 940 3.20000000 RUS2004N1030487
117 RUS 2004 m 24 1040 3.20000000 RUS2004N1030487
118 NL 2015 m 24 300 4.06293706 NL2015E278
119 NL 2012 m 24 258 -8.13350126 NL2012Eun7
120 NL 2012 f 24 190 3.86649874 NL2012E239
121 RUS 2014 m 24 1085 2.28608247 RUS2014N858
122 RUS 2003 m 24 1100 1.20000000 RUS2003N1030333
123 RUS 2004 m 24 610 -0.70000000 RUS2004N1040622
124 RUS 2004 m 24 674 -0.20000000 RUS2004N1040444
125 RUS 2004 m 24 772 -0.20000000 RUS2004N1040285
126 RUS 2004 m 24 760 -0.20000000 RUS2004N1040571
127 RUS 2005 m 24 1060 0.80000000 RUS2005N1040118
139 RUS 2014 f 25 860 4.28608247 RUS2014N766
140 RUS 2014 m 25 1040 4.28608247 RUS2014N766
142 NL 2015 f 25 490 -0.93706294 NL2015W224
143 RUS 2014 m 25 1002 1.28608247 RUS2014NT1
144 RUS 2014 f 25 880 1.28608247 RUS2014N812
145 RUS 2014 m 25 955 1.28608247 RUS2014N111
146 RUS 2014 m 25 985 1.28608247 RUS2014N111
147 RUS 2003 f 25 980 -0.80000000 RUS2003N1030045
148 RUS 2004 f 25 1070 0.20000000 RUS2004N1030466
149 RUS 2004 m 25 1050 0.20000000 RUS2004N1030439
150 RUS 2004 f 25 940 0.20000000 RUS2004N1030439
151 RUS 2004 f 25 900 0.20000000 RUS2004N1030461
152 RUS 2014 m 25 985 1.28608247 RUS2014N602
153 RUS 2004 m 25 560 -1.20000000 RUS2004N1040503
154 RUS 2004 f 25 680 0.20000000 RUS2004N1030437
155 RUS 2004 f 25 910 0.20000000 RUS2004N1030451
156 RUS 2004 m 25 484 0.80000000 RUS2004N1040541
158 RUS 2014 f 26 985 0.28608247 RUS2014N61
159 RUS 2014 f 26 882 0.28608247 RUS2014N61
160 RUS 2004 m 26 626 -0.20000000 RUS2004N1040474
161 RUS 2003 f 26 900 -0.80000000 RUS2003N1030429
162 RUS 2004 m 26 810 3.20000000 RUS2004N1030476
163 RUS 2014 f 26 860 0.28608247 RUS2014N706
164 RUS 2014 f 26 945 2.28608247 RUS2014N963
165 NL 2004 f 26 480 23.00000000 NL2004N1040293
166 RUS 2004 f 26 720 -0.70000000 RUS2004N1040832
167 RUS 2004 m 26 658 1.30000000 RUS2004N1042086
168 RUS 2004 f 26 634 1.80000000 RUS2004N1042149
171 RUS 2004 f 27 1300 -1.80000000 RUS2004N1030455
172 RUS 2014 f 27 800 -0.71391753 RUS2014N619
173 RUS 2014 m 27 1020 -0.71391753 RUS2014N411
174 RUS 2014 m 27 1040 -0.71391753 RUS2014N411
175 RUS 2014 f 27 980 3.28608247 RUS2014N1119
176 RUS 2014 m 27 990 3.28608247 RUS2014N1119
177 RUS 2014 m 27 925 3.28608247 RUS2014N1119
178 RUS 2003 f 27 762 -1.80000000 RUS2003N1030400
179 RUS 2003 f 27 880 -1.80000000 RUS2003N1030415
180 RUS 2003 f 27 1020 -1.80000000 RUS2003N1030406
181 RUS 2003 f 27 950 -1.80000000 RUS2003N1030406
182 RUS 2003 m 27 1060 -1.80000000 RUS2003N1030406
183 RUS 2003 m 27 772 0.20000000 RUS2003N1030400
184 NL 2015 m 27 525 1.06293706 NL2015E222
185 RUS 2014 f 27 830 2.28608247 RUS2014N769
186 RUS 2014 f 27 940 2.28608247 RUS2014N769
187 RUS 2003 m 27 976 -1.80000000 RUS2003N1030402
188 RUS 2003 f 27 1050 -1.80000000 RUS2003N1030413
189 RUS 2003 m 27 1090 -1.80000000 RUS2003N1030414
190 RUS 2003 f 27 1140 -1.80000000 RUS2003N1030413
191 RUS 2003 f 27 1080 -1.80000000 RUS2003N1030414
192 RUS 2004 f 27 630 3.20000000 RUS2004N1030475
193 NL 2012 m 27 290 0.86649874 NL2012W103
194 RUS 2014 m 27 1100 -0.71391753 RUS2014N519
195 RUS 2014 m 27 985 -0.71391753 RUS2014N629
196 RUS 2014 m 27 925 1.28608247 RUS2014N533
197 RUS 2014 f 27 950 2.28608247 RUS2014N452
198 RUS 2003 f 27 1060 -1.80000000 RUS2003N1030411
199 RUS 2005 f 27 990 3.10000000 RUS2005N1050621
204 NL 2012 f 28 237 -0.13350126 NL2012E262
206 RUS 2014 m 28 1085 -1.71391753 RUS2014N321
207 RUS 2014 m 28 1220 -1.71391753 RUS2014N321
208 RUS 2014 m 28 1010 -1.71391753 RUS2014N321
209 RUS 2014 f 28 1005 2.28608247 RUS2014N554
210 RUS 2014 m 28 1040 2.28608247 RUS2014N554
211 RUS 2014 f 28 1005 2.28608247 RUS2014N554
212 RUS 2003 f 28 960 -0.80000000 RUS2003N1030429
213 RUS 2003 m 28 1330 -0.80000000 RUS2003N1030100
214 RUS 2003 f 28 1140 -0.80000000 RUS2003N1030100
215 RUS 2003 m 28 1340 -0.80000000 RUS2003N1030100
216 RUS 2004 m 28 586 -2.20000000 RUS2004N1040279
217 RUS 2005 m 28 894 0.80000000 RUS2005N1040060
218 RUS 2014 m 28 980 -1.71391753 RUS2014N631
219 RUS 2014 f 28 790 1.28608247 RUS2014NT1
220 RUS 2003 f 28 902 -2.80000000 RUS2003N1030196
221 RUS 2004 f 28 744 -3.20000000 RUS2004N1040600
222 RUS 2005 m 28 576 0.80000000 RUS2005N1040130
223 RUS 2005 f 28 517 0.80000000 RUS2005N1040130
224 RUS 2014 f 28 855 2.28608247 RUS2014N1171
225 RUS 2003 f 28 740 -0.80000000 RUS2003N1030090
226 RUS 2004 m 28 792 -2.20000000 RUS2004N1040381
227 RUS 2005 m 28 1034 0.80000000 RUS2005N1040265
231 NL 2015 f 29 430 -0.93706294 NL2015W228
232 NL 2015 f 29 425 -0.93706294 NL2015W228
233 RUS 2014 m 29 843 -2.71391753 RUS2014N418
234 RUS 2014 f 29 1020 -2.71391753 RUS2014N418
235 RUS 2014 m 29 1100 -4.71391753 RUS2014N123
236 RUS 2014 f 29 1100 -4.71391753 RUS2014N123
237 RUS 2014 m 29 1000 -2.71391753 RUS2014N113
238 RUS 2014 m 29 1160 -2.71391753 RUS2014N113
239 RUS 2014 f 29 1000 -2.71391753 RUS2014N113
240 RUS 2014 f 29 1055 -2.71391753 RUS2014N286
241 RUS 2014 f 29 1100 -2.71391753 RUS2014N286
242 RUS 2014 f 29 1125 -0.71391753 RUS2014NT7
243 RUS 2014 f 29 985 -0.71391753 RUS2014N284
244 RUS 2004 f 29 612 0.80000000 RUS2004N1040528
245 RUS 2005 m 29 624 -0.20000000 RUS2005N1040150
246 RUS 2005 f 29 510 -0.20000000 RUS2005N1040150
247 RUS 2005 m 29 1000 -0.20000000 RUS2005N1040151
248 RUS 2005 f 29 902 -0.20000000 RUS2005N1040151
249 RUS 2014 f 29 1115 -2.71391753 RUS2014N672
250 RUS 2014 f 29 1245 -0.71391753 RUS2014N616
251 RUS 2014 m 29 1325 -0.71391753 RUS2014N239
252 RUS 2004 f 29 860 3.20000000 RUS2004N1030484
253 RUS 2005 m 29 708 -0.70000000 RUS2005N1040163
254 RUS 2005 m 29 618 -0.20000000 RUS2005N1040082
255 RUS 2005 f 29 910 -0.20000000 RUS2005N1040137
264 RUS 2014 m 30 1170 0.28608247 RUS2014N61
265 RUS 2004 f 30 1090 0.20000000 RUS2004N1030467
266 RUS 2014 f 30 1030 -0.71391753 RUS2014N866
267 RUS 2014 m 30 1025 -0.71391753 RUS2014N619
268 RUS 2014 m 30 1120 7.28608247 RUS2014N1016
269 RUS 2014 f 30 1000 7.28608247 RUS2014N1016
270 RUS 2014 f 30 890 7.28608247 RUS2014N1016
271 RUS 2004 m 30 954 -2.20000000 RUS2004N1040606
272 RUS 2004 f 30 965 -2.20000000 RUS2004N1040606
273 RUS 2004 m 30 948 -2.20000000 RUS2004N1040606
274 RUS 2005 m 30 1238 -1.20000000 RUS2005N1040217
275 RUS 2005 m 30 1260 -1.20000000 RUS2005N1040217
276 RUS 2005 f 30 1146 -1.20000000 RUS2005N1040217
277 RUS 2014 m 30 1182 -3.71391753 RUS2014N112
279 RUS 2014 m 30 1260 -3.71391753 RUS2014N112
281 RUS 2014 f 30 1200 -3.71391753 RUS2014N364
282 RUS 2014 m 30 1110 -0.71391753 RUS2014N221
283 RUS 2014 m 30 1235 -0.71391753 RUS2014N221
284 RUS 2004 m 30 880 0.20000000 RUS2004N1030442
285 RUS 2004 f 30 720 0.20000000 RUS2004N1030442
286 RUS 2014 m 30 1022 -3.71391753 RUS2014N238
287 RUS 2014 m 30 1120 -3.71391753 RUS2014N270
288 RUS 2014 m 30 1175 -3.71391753 RUS2014N647
289 RUS 2014 f 30 1100 -3.71391753 RUS2014N288
290 RUS 2014 f 30 980 1.28608247 RUS2014N1032
291 RUS 2004 f 30 840 0.20000000 RUS2004N1030449
292 RUS 2005 m 30 1080 -1.40000000 RUS2005N1050085
294 NL 2012 f 31 430 -3.13350126 NL2012W27
295 NL 2012 m 31 405 -3.13350126 NL2012W27
296 NL 2015 m 31 810 -2.93706294 NL2015E149
297 NL 2015 m 31 740 -2.93706294 NL2015E149
298 RUS 2014 f 31 1140 -0.71391753 RUS2014N914
299 RUS 2014 m 31 1255 -0.71391753 RUS2014N914
300 RUS 2014 f 31 1225 -0.71391753 RUS2014N914
301 RUS 2014 m 31 1020 -0.71391753 RUS2014N411
302 RUS 2014 m 31 1255 2.28608247 RUS2014N698
303 RUS 2014 m 31 1235 2.28608247 RUS2014N698
304 RUS 2014 m 31 1120 2.28608247 RUS2014N698
305 RUS 2004 m 31 1204 -0.70000000 RUS2004N1040833
306 RUS 2004 f 31 1024 -0.70000000 RUS2004N1040833
307 RUS 2004 m 31 790 3.20000000 RUS2004N1030475
308 RUS 2004 m 31 824 1.30000000 RUS2004N1042120
309 RUS 2004 f 31 704 1.30000000 RUS2004N1042142
318 NL 2012 f 32 418 -4.13350126 NL2012W70
319 NL 2012 m 32 410 -4.13350126 NL2012W70
320 RUS 2004 m 32 1340 -1.80000000 RUS2004N1030455
321 RUS 2005 f 32 1140 2.10000000 RUS2005N1050173
322 RUS 2005 m 32 1330 2.10000000 RUS2005N1050173
323 RUS 2005 m 32 1300 2.10000000 RUS2005N1050173
324 RUS 2005 m 32 1210 2.10000000 RUS2005N1050173
325 RUS 2005 f 32 1070 2.10000000 RUS2005N1050173
326 NL 2015 m 32 810 -3.93706294 NL2015W40
327 NL 2012 m 32 408 -4.13350126 NL2012E25
328 NL 2012 f 32 282 -4.13350126 NL2012E25
330 RUS 2005 f 32 1050 2.10000000 RUS2005N1050505
331 RUS 2005 f 32 1050 2.10000000 RUS2005N1050505
332 RUS 2005 f 32 930 2.10000000 RUS2005N1050505
333 RUS 2014 f 32 1125 -3.71391753 RUS2014N91
334 RUS 2014 m 32 1402 -3.71391753 RUS2014N91
335 RUS 2014 f 32 1085 -1.71391753 RUS2014N631
336 RUS 2003 f 32 1140 -0.80000000 RUS2003N1030424
337 RUS 2003 m 32 1100 -0.80000000 RUS2003N1030424
338 RUS 2004 m 32 1010 0.20000000 RUS2004N1030457
339 RUS 2005 m 32 1008 -3.20000000 RUS2005N1040180
340 RUS 2005 f 32 830 -3.20000000 RUS2005N1040180
342 RUS 2014 f 32 1140 -5.71391753 RUS2014N117
343 NL 2004 f 32 530 17.50000000 NL2004N1041011
344 RUS 2004 f 32 640 0.20000000 RUS2004N1030459
348 RUS 2014 m 33 1265 -3.71391753 RUS2014N293
349 RUS 2014 m 33 1190 -3.71391753 RUS2014N293
350 RUS 2014 m 33 1225 -3.71391753 RUS2014N293
351 RUS 2014 f 33 1090 -3.71391753 RUS2014N293
352 RUS 2004 m 33 1160 -0.80000000 RUS2004N1030467
354 RUS 2003 m 33 980 -1.80000000 RUS2003N1030415
355 RUS 2004 f 33 476 -2.20000000 RUS2004N1040279
356 RUS 2004 m 33 554 -2.20000000 RUS2004N1040279
357 RUS 2014 f 33 1310 4.28608247 RUS2014N1000
358 RUS 2014 f 33 1060 4.28608247 RUS2014N1000
359 RUS 2003 m 33 842 -1.80000000 RUS2003N1030418
360 RUS 2005 m 33 1050 -2.90000000 RUS2005N1050580
361 RUS 2005 m 33 1226 -2.90000000 RUS2005N1050580
362 RUS 2014 m 33 1190 -2.71391753 RUS2014N175
363 RUS 2014 f 33 1105 4.28608247 RUS2014N755
364 RUS 2014 m 33 1260 4.28608247 RUS2014N801
365 RUS 2003 f 33 850 -1.80000000 RUS2003N1030422
367 RUS 2004 m 33 1054 -2.20000000 RUS2004N1040478
368 RUS 2004 f 33 810 0.20000000 RUS2004N1030456
369 RUS 2004 m 33 944 1.80000000 RUS2004N1041049
370 RUS 2004 m 33 1102 1.30000000 RUS2004N1042127
371 RUS 2004 f 33 1050 1.80000000 RUS2004N1040821
372 RUS 2004 f 33 935 1.80000000 RUS2004N1042131
373 RUS 2005 m 33 868 -4.20000000 RUS2005N1040266
376 NL 2012 m 34 270 -6.13350126 NL2012W15
377 NL 2012 f 34 438 -6.13350126 NL2012W15
379 NL 2012 m 34 440 -6.13350126 NL2012Wun15
380 NL 2012 f 34 440 -6.13350126 NL2012Wun15
383 RUS 2014 f 34 990 -4.71391753 RUS2014N216
384 RUS 2014 f 34 1220 -3.71391753 RUS2014N106
385 RUS 2014 f 34 1090 -3.71391753 RUS2014N106
386 RUS 2014 m 34 1240 -0.71391753 RUS2014NT7
387 RUS 2014 f 34 1085 3.28608247 RUS2014N896
388 RUS 2014 m 34 1110 3.28608247 RUS2014N896
389 RUS 2004 m 34 1074 -1.20000000 RUS2004N1040269
390 RUS 2004 f 34 980 -1.20000000 RUS2004N1040269
391 RUS 2004 f 34 930 0.20000000 RUS2004N1030466
392 RUS 2004 f 34 1042 -3.20000000 RUS2004N1040455
393 RUS 2004 m 34 815 0.80000000 RUS2004N1040516
399 RUS 2014 m 35 1275 -1.71391753 RUS2014N332
400 RUS 2014 f 35 1210 -1.71391753 RUS2014N332
401 RUS 2014 m 35 1230 -1.71391753 RUS2014N332
402 RUS 2014 m 35 1380 2.28608247 RUS2014N733
403 RUS 2014 m 35 1410 2.28608247 RUS2014N733
404 RUS 2014 f 35 1270 2.28608247 RUS2014N733
405 RUS 2014 f 35 1040 2.28608247 RUS2014N1102
406 RUS 2014 f 35 1080 2.28608247 RUS2014N1102
407 RUS 2005 f 35 1150 -0.90000000 RUS2005N1051024
408 RUS 2005 m 35 1390 -0.90000000 RUS2005N1051024
409 NL 2012 m 35 455 -7.13350126 NL2012E121
410 RUS 2014 m 35 1250 -3.71391753 RUS2014N397
411 NL 2005 m 35 990 1.30000000 NL2005N1050244
412 RUS 2004 f 35 1020 -2.20000000 RUS2004N1040437
413 RUS 2005 m 35 1200 -6.40000000 RUS2005N1050422
418 RUS 2004 m 36 1320 -1.80000000 RUS2004N1030455
419 RUS 2004 m 36 1270 -1.80000000 RUS2004N1030455
420 RUS 2004 m 36 1430 -1.80000000 RUS2004N1030455
421 NL 2012 f 36 460 -8.13350126 NL2012Wun5
422 NL 2012 m 36 505 -8.13350126 NL2012Wun5
424 RUS 2004 m 36 916 -3.20000000 RUS2004N1040600
425 RUS 2014 m 36 1300 1.28608247 RUS2014N1451
426 RUS 2005 m 36 1420 -1.40000000 RUS2005N1050054
427 RUS 2005 f 36 1170 -1.90000000 RUS2005N1050270
432 RUS 2013 f 37 1030 -4.78111588 RUS2013N407
433 RUS 2013 f 37 1050 -4.78111588 RUS2013N407
434 RUS 2013 m 37 1140 -4.78111588 RUS2013N407
439 RUS 2014 f 37 1250 0.28608247 RUS2014N422
440 RUS 2004 f 37 1120 -2.20000000 RUS2004N1040480
447 RUS 2014 m 38 1615 -4.71391753 RUS2014N351
448 RUS 2014 f 38 1430 -4.71391753 RUS2014N351
449 RUS 2014 f 38 1225 -0.71391753 RUS2014N386
450 RUS 2014 f 38 1315 -0.71391753 RUS2014N386
451 NL 2015 m 38 940 -2.93706294 NL2015E117
452 NL 2012 m 38 1090 -5.13350126 NL2012E130
453 RUS 2014 f 38 1225 -0.71391753 RUS2014N668
454 RUS 2014 m 38 1430 -0.71391753 RUS2014N493
461 NL 2012 f 39 535 -11.13350126 NL2012E31
462 NL 2012 f 39 195 -11.13350126 NL2012Wun1
463 RUS 2014 m 39 1440 -1.71391753 RUS2014N202
464 RUS 2014 m 39 1250 -1.71391753 RUS2014N202
465 NL 2015 m 39 600 2.06293706 NL2015W280
466 NL 2012 f 39 1090 16.86649874 NL2012E323
467 RUS 2014 f 39 1325 -1.71391753 RUS2014N266
468 NL 2004 m 39 1050 9.00000000 NL2004N1040299
475 RUS 2014 m 40 1480 -2.71391753 RUS2014N300
476 RUS 2014 m 40 1450 -2.71391753 RUS2014N300
477 RUS 2014 m 40 1475 -2.71391753 RUS2014N300
478 RUS 2014 f 40 1230 -2.71391753 RUS2014N656
479 RUS 2014 f 40 1300 -2.71391753 RUS2014N207
480 RUS 2014 m 40 1480 -2.71391753 RUS2014N207
481 RUS 2014 m 40 1310 -2.71391753 RUS2014N656
482 NL 2012 m 40 805 -8.13350126 NL2012W39
483 RUS 2014 m 40 1428 -2.71391753 RUS2014N273
491 RUS 2014 m 41 1110 -3.71391753 RUS2014N615
492 NL 2004 f 41 1000 7.00000000 NL2004N1040255
502 NL 2012 f 44 700 -6.13350126 NL2012W15
503 NL 2005 m 44 1160 -7.20000000 NL2005N1050197
504 NL 2005 m 44 1020 -7.20000000 NL2005N1050197
505 NL 2005 m 44 1240 -7.20000000 NL2005N1050197
506 NL 2005 f 44 1050 -7.20000000 NL2005N1050197
508 NL 2012 m 45 710 -7.13350126 NL2012W9
509 NL 2012 f 45 680 -7.13350126 NL2012W9
510 NL 2012 m 47 1100 -2.13350126 NL2012W154
511 NL 2015 m 47 630 -5.93706294 NL2015W202
512 NL 2012 m 47 1130 -2.13350126 NL2012E197
513 NL 2004 f 47 1210 1.00000000 NL2004N1040149
514 NL 2004 f 47 1250 1.00000000 NL2004N1040157
519 NL 2004 f 50 1320 -2.00000000 NL2004N1040129
520 NL 2004 m 50 1380 -2.00000000 NL2004N1040129
521 NL 2012 f 50 1060 -8.13350126 NL2012E111
522 NL 2004 m 50 1510 -2.00000000 NL2004N1040119
523 NL 2004 f 50 940 -1.50000000 NL2004N1040102
524 NL 2012 m 51 625 -3.13350126 NL2012W27
525 NL 2012 f 51 1325 -6.13350126 NL2012E28
527 NL 2012 f 52 1220 -1.13350126 NL2012E154
528 NL 2004 m 52 1160 -5.00000000 NL2004N1040134
529 NL 2012 m 53 1200 -2.13350126 NL2012W154
530 NL 2012 f 53 880 -5.13350126 NL2012Wun9
531 NL 2012 f 53 880 -5.13350126 NL2012Wun9
532 NL 2004 m 53 1290 -5.00000000 NL2004N1040106
533 NL 2004 m 53 1230 -5.00000000 NL2004N1040106
534 NL 2012 m 53 790 -5.13350126 NL2012Wun12
535 NL 2012 m 53 1130 -2.13350126 NL2012W57
536 NL 2012 f 53 1300 2.86649874 NL2012Wun24
537 NL 2004 m 53 1530 -5.00000000 NL2004N1040063
538 NL 2004 m 54 1450 -5.00000000 NL2004N1040054
539 NL 2004 m 54 1060 -5.00000000 NL2004N1040054
540 NL 2004 f 54 1110 -5.00000000 NL2004N1040054
541 NL 2012 f 54 695 -6.13350126 NL2012E131
542 NL 2012 m 54 935 -6.13350126 NL2012W115
543 NL 2012 f 54 640 -6.13350126 NL2012E131
544 NL 2012 f 54 830 -6.13350126 NL2012W115
545 NL 2004 m 54 1380 -5.00000000 NL2004N1040057
546 NL 2004 f 54 910 -5.00000000 NL2004N1041151
550 NL 2012 m 56 1325 -11.13350126 NL2012E45
551 NL 2012 m 56 900 -5.13350126 NL2012E32
552 NL 2012 f 56 1225 -5.13350126 NL2012E32
553 NL 2015 m 56 1510 0.06293706 NL2015E209
554 NL 2012 f 56 1280 -3.13350126 NL2012W203
555 NL 2012 f 57 1180 -4.13350126 NL2012E25
556 NL 2015 m 57 1180 -7.93706294 NL2015Wu
557 NL 2015 f 57 1080 -0.93706294 NL2015W348
559 NL 2015 f 58 980 -1.93706294 NL2015E93
560 NL 2015 f 58 1040 -1.93706294 NL2015Eu
562 NL 2015 f 59 1040 -2.93706294 NL2015W216
563 NL 2015 f 59 1030 -2.93706294 NL2015W216
564 NL 2015 f 59 1140 -2.93706294 NL2015W216
565 NL 2015 m 59 1440 -2.93706294 NL2015E66
566 NL 2012 f 59 1360 -8.13350126 NL2012W35
567 NL 2012 m 60 1070 -15.13350126 NL2012W49
569 NL 2012 m 62 1400 -11.13350126 NL2012E37")
bmgrp <- groupedData(bm ~ age | year, data = bmdata)
options(contrasts = c("contr.sum","contr.poly"))
contrasts(bmgrp$pop); contrasts(bmgrp$sex)
startvalues <- c(31.228925, 75.552237, 12.708210, 9.427097)

Some further search in the internet brought me to the following article: Zhou et al. (2007): Using a nonlinear crossed random effects model for describing circadian patterns of serum prolactin concentrations in heat stressed Holsteins
(http://newprairiepress.org/cgi/viewcontent.cgi?article=1113&context=agstatconference). They write the following about crossed random effects in nlme:
Fitting nonlinear mixed models with both crossed and nested random effects is still a new and challenging topic in statistics. […] The NLME package in R is very powerful for fitting multilevel nonlinear mixed-effects
models with nested random effects, but it does not fit nonlinear mixed-effects models
with crossed random effects (Pinheiro and Bates, 2000). Rasbash and Goldstein (1994)
show how to fit a linear mixed model with crossed random effects as a purely
hierarchical formulation of nested random effects. Zhou, Parkhurst, et al (2006) (http://newprairiepress.org/agstatconference/2006/proceedings/11/) developed a method to enable NLME in R to fit a nonlinear mixed-effects model with crossed random effects based on Rasbash and Goldstein’s idea for linear mixed-effects model. This method can be used to fit a nonlinear mixed effects model with arbitrary levels of crossed and nested random effects. However, only one correlation for the crossed random effects can be estimated. The NLME code for fitting the nonlinear mixed-effects model […] is documented in the Appendix.
# Feeding Event is specified as the lowest hierarchical level grouping factor
# (hierarchical level 1 grouping factor)
# Specify Pig as a standard hierarchical level 2 grouping factor
# Create a new grouping factor as the highest hierarchical level
# (hierarchical level 3 grouping factor) with one level spanning the entire data set
newGF <- factor(rep(1,length(heat$Y)))
# Specify that each level of Period has a coefficient random at newGF
# “ -1” indicates that the specific term factor(Period ) does not have an intercept
# It is not possible to estimate the correlation associated with Period.
full.nlme <-
nlme(model = Y ~ Y0 + KG * (exp(-KD * X) - exp(-KG * X))/(KG - KD),
fixed = Y0 + KG + KD ~ factor(Trt),
random = list(newGF = pdIdent(Y0 ~ factor(Period) -1),
newGF = pdIdent(KG ~ factor(Period) -1),
newGF = pdIdent(KD ~ factor(Period) -1),
Pig = pdDiag(Y0 + KG + KD ~ 1),
Event = pdDiag(Y0 + KG + KD ~ 1)),
start = c(39, 0, 0, 60, 0, 0, 20, 0, 0), data = heat)
I applied this syntax to my case - and it worked!
Here it is:
fmbm1 <- nlme(bm ~ SSlogis(age, Asym, xmid, scal), data = bmgrp,
fixed = Asym + xmid + scal ~ 1,
random = list(dummy = pdIdent(Asym ~ factor(year) - 1),
dummy = pdIdent(xmid ~ factor(year) - 1),
dummy = pdIdent(scal ~ factor(year) - 1),
brood = pdDiag(Asym + xmid + scal ~ 1)),
start = coef(fmbml), na.action = na.omit)
> summary(fmbm1)
Nonlinear mixed-effects model fit by maximum likelihood
Model: bm ~ SSlogis(age, Asym, xmid, scal)
Data: bmgrp
AIC BIC logLik
5505.905 5546.235 -2742.952
Random effects:
Formula: Asym ~ factor(year) - 1 | dummy
Structure: Multiple of an Identity
Asym.factor(year)2013 Asym.factor(year)2012 Asym.factor(year)2003 Asym.factor(year)2005 Asym.factor(year)2015 Asym.factor(year)2004
StdDev: 0.004011662 0.004011662 0.004011662 0.004011662 0.004011662 0.004011662
Asym.factor(year)2014
StdDev: 0.004011662
Formula: xmid ~ factor(year) - 1 | dummy %in% dummy
Structure: Multiple of an Identity
xmid.factor(year)2013 xmid.factor(year)2012 xmid.factor(year)2003 xmid.factor(year)2005 xmid.factor(year)2015 xmid.factor(year)2004
StdDev: 8.630687 8.630687 8.630687 8.630687 8.630687 8.630687
xmid.factor(year)2014
StdDev: 8.630687
Formula: scal ~ factor(year) - 1 | dummy %in% dummy %in% dummy
Structure: Multiple of an Identity
scal.factor(year)2013 scal.factor(year)2012 scal.factor(year)2003 scal.factor(year)2005 scal.factor(year)2015 scal.factor(year)2004
StdDev: 1.890416 1.890416 1.890416 1.890416 1.890416 1.890416
scal.factor(year)2014
StdDev: 1.890416
Formula: list(Asym ~ 1, xmid ~ 1, scal ~ 1)
Level: brood %in% dummy %in% dummy %in% dummy
Structure: Diagonal
Asym xmid scal Residual
StdDev: 2.176433e-05 5.079293 0.001926683 99.83194
Fixed effects: Asym + xmid + scal ~ 1
Value Std.Error DF t-value p-value
Asym.(Intercept) 1721.7518 123.05426 153 13.991810 0
xmid.(Intercept) 32.1919 3.83007 153 8.405047 0
scal.(Intercept) 11.5256 1.36451 153 8.446679 0
Correlation:
As.(I) xm.(I)
xmid.(Intercept) 0.495
scal.(Intercept) 0.734 0.369
Standardized Within-Group Residuals:
Min Q1 Med Q3 Max
-2.20621393 -0.43418898 0.01076569 0.40318446 2.34353430
Number of Observations: 417
Number of Groups:
dummy dummy.1 %in% dummy dummy.2 %in% dummy.1 %in% dummy
1 1 1
brood %in% dummy.2 %in% dummy.1 %in% dummy
262
>

Related

Radix Sort doesn't work properly in 1d array (c)

I'm trying to radix sort 1d array by growth, but it doesn't sort: it displays the same arrays. When I'm trying to find mistakes, compiler says there are no ones.
My code is (I used an example given on Programiz site to write Maximum and radix parts: https://www.programiz.com/dsa/radix-sort):
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <ctype.h>
int a[1000];
int n;
int place;
int k = 0, t = 0; // k - value of sorting, t - value of swaps
int Maximum (int a[1000], int n) {
int max = a[0];
for (int i = 0; i < n; i++)
{
k++;
if (a[i] > max)
{
max = a[i];
t++;
}
}
return max;
}
void Counting (int a[1000], int n, int place) {
int max = (a[0] / place) % 10;
for (int i = 1; i < n; i++)
{
k++;
if (((a[i] / place) % 10) > place)
{
max = a[i];
t++;
}
}
int count[max+1];
for (int i = 0; i < max; i++)
{
count[i] = 0;
}
for (int i = 0; i < n; i++)
{
count[(a[i]/place) % 10]++;
}
for (int i = 0; i < 10; i++)
{
count[i] += count[i-1];
}
int output[n+1];
for (int i = n-1; i >= 0; i--)
{
output[count[(a[i] / place) % 10] -1] = a[i];
count[(a[i]/place) % 10]--;
}
for (int i = 0; i < n; i++)
{
a[i] = output[i];
}
}
void radix(int a[1000], int n) {
int max = Maximum(a, n);
for (int place = 1; max/place > 0; place *= 10)
{
Counting(a, n, place);
}
}
int main() {
int i;
int a[1000];
srand (time (NULL));
for ( i = 0; i < 1000; i++)
{
a[i] = rand() % 1000;
}
printf ("Array: \n");
for ( i = 0; i < 1000; i++)
{
printf ("%4.0d", a[i]);
}
printf ("\n");
printf ("Sorted array: \n");
radix(a, n);
for ( i = 0; i < 1000; i++)
{
printf ("%4.0d", a[i]);
}
printf ("\n");
printf ("k = %d", k);
printf ("\nt = %d", t);
return 0;
}
The result displayed is:
Array:
264 843 736 441 404 801 478 517 81 328 424 935 241 585 97 556 453 118 950 305 977 626 450 829 828 507 197 164 278 16 756 295 898 652 973 103 784 422 355 782 466 931 546 958 205 61 700 777 323 214 526 697 86 717 642 279 134 770 166 477 481 506 130 913 24 59 52 391 195 570 526 670 706 409 371 566 211 991 407 981 726 726 399 172 29 234 602 543 287 605 381 231 141 123 345 889 349 988 720 39 361 623 571 928 934 944 942 615 871 358 331 727 347 699 262 218 177 328 892 689 494 678 615 640 328 246 421 218 233 807 568 890 831 979 621 725 366 193 488 177 197 870 210 383 682 701 957 805 70 877 708 765 316 426 71 882 624 418 976 145 49 870 680 4 537 937 190 4 534 499 538 185 924 396 572 286 438 349 983 271 999 921 967 539 819 808 649 399 802 962 491 744 794 890 107 162 816 725 727 366 94 677 377 4 930 958 303 31 574 112 753 445 309 393 461 365 435 975 366 205 272 686 613 906 702 67 24 717 923 246 752 696 397 568 689 457 728 567 838 517 903 680 86 792 68 290 398 506 794 146 919 308 990 435 501 78 273 878 989 480 108 275 713 995 912 818 878 30 107 784 782 403 950 5 564 853 635 748 350 680 606 261 548 985 318 907 45 203 751 77 641 998 829 857 925 545 618 458 510 307 615 660 415 176 808 265 693 486 707 436 733 263 133 952 769 919 760 780 325 754 300 910 827 798 163 502 74 73 464 671 920 194 406 785 356 287 635 496 442 712 543 943 187 795 168 641 131 327 901 754 18 598 570 909 782 682 416 703 792 834 470 513 611 26 746 205 250 930 866 248 747 424 191 825 980 943 138 6 34 7 122 640 22 432 337 660 808 300 444 161 250 771 408 544 134 8 961 347 422 141 16 708 433 64 754 704 664 160 332 691 441 117 80 310 8 49 316 6 954 281 399 113 202 974 590 99 78 89 590 612 280 740 990 147 348 750 341 401 71 695 396 376 4 113 542 224 221 83 825 880 969 233 343 824 860 28 998 808 399 122 536 305 928 439 278 70 419 842 379 766 342 779 651 20 64 479 259 514 206 712 543 983 647 865 956 760 790 316 822 528 589 375 230 132 624 580 921 610 542 287 626 185 707 496 743 697 240 876 901 441 931 864 9 387 350 600 846 591 42 384 730 435 147 975 912 370 296 520 173 372 985 75 469 350 630 932 352 303 969 542 915 715 457 111 384 915 208 527 453 175 9 995 69 979 963 854 838 763 592 546 366 14 297 353 343 187 729 769 488 77 701 556 842 385 148 434 886 805 534 628 734 678 854 491 160 801 999 247 261 656 820 65 986 133 228 927 870 700 160 332 674 271 619 473 988 880 933 670 102 743 223 177 121 241 195 956 970 573 226 329 955 708 607 628 78 801 906 494 311 497 280 363 718 98 395 494 994 646 781 697 788 671 831 679 70 637 374 597 578 213 243 951 489 635 900 739 397 729 400 578 377 171 251 553 823 555 522 924 582 705 500 312 345 515 829 631 804 376 301 134 226 448 539 659 265 306 572 863 715 552 13 51 842 426 546 137 283 491 971 52 47 391 190 430 664 170 22 413 505 990 460 385 831 934 972 30 763 892 387 98 27 510 701 253 365 486 638 86 836 547 936 341 713 772 482 355 51 521 460 997 770 897 836 270 672 578 788 879 574 439 15 416 132 4 957 39 994 756 588 300 494 952 963 212 59 109 931 22 731 694 855 69 263 573 542 82 367 612 48 747 285 748 989 263 640 758 708 208 787 114 235 144 304 331 205 236 126 934 869 30 475 889 597 575 992 287 23 112 670 672 687 602 576 211 105 724 409 227 760 8 725 481 973 610 379 119 491 877 298 390 94 694 231 404 577 106 687 145 546 164 331 379 353 952 971 666 219 58 352 105 812 475 572 664 174 584 260 279 694 363 251 8 133 45 648 691 964 273 216 489 917 888 758 227 359 652 918 539 801 18 653 378 958 836 44 463 375 458 525 694 173 300 954 670 457 459 766 822 303 187 118 946 630 748 819 697 620 675 484 795 89 36 763 628 278 37 660 689 468 640 434 261 641 514 230 25 196 642 358 197 629 192 534 842 935 26 757 656 682 292 621 318 168 425 497 456 127 227 373 651 798 609 179 924 285 603 74 252 312 688 25 687 947 754 635 444 773 63 312 145 665 201 892 635 341 952 410 675 632 623 738 471 506 464 913 829 942 945 135 894 579 245 762 737 989 129 487 391 677 338
Sorted array:
264 843 736 441 404 801 478 517 81 328 424 935 241 585 97 556 453 118 950 305 977 626 450 829 828 507 197 164 278 16 756 295 898 652 973 103 784 422 355 782 466 931 546 958 205 61 700 777 323 214 526 697 86 717 642 279 134 770 166 477 481 506 130 913 24 59 52 391 195 570 526 670 706 409 371 566 211 991 407 981 726 726 399 172 29 234 602 543 287 605 381 231 141 123 345 889 349 988 720 39 361 623 571 928 934 944 942 615 871 358 331 727 347 699 262 218 177 328 892 689 494 678 615 640 328 246 421 218 233 807 568 890 831 979 621 725 366 193 488 177 197 870 210 383 682 701 957 805 70 877 708 765 316 426 71 882 624 418 976 145 49 870 680 4 537 937 190 4 534 499 538 185 924 396 572 286 438 349 983 271 999 921 967 539 819 808 649 399 802 962 491 744 794 890 107 162 816 725 727 366 94 677 377 4 930 958 303 31 574 112 753 445 309 393 461 365 435 975 366 205 272 686 613 906 702 67 24 717 923 246 752 696 397 568 689 457 728 567 838 517 903 680 86 792 68 290 398 506 794 146 919 308 990 435 501 78 273 878 989 480 108 275 713 995 912 818 878 30 107 784 782 403 950 5 564 853 635 748 350 680 606 261 548 985 318 907 45 203 751 77 641 998 829 857 925 545 618 458 510 307 615 660 415 176 808 265 693 486 707 436 733 263 133 952 769 919 760 780 325 754 300 910 827 798 163 502 74 73 464 671 920 194 406 785 356 287 635 496 442 712 543 943 187 795 168 641 131 327 901 754 18 598 570 909 782 682 416 703 792 834 470 513 611 26 746 205 250 930 866 248 747 424 191 825 980 943 138 6 34 7 122 640 22 432 337 660 808 300 444 161 250 771 408 544 134 8 961 347 422 141 16 708 433 64 754 704 664 160 332 691 441 117 80 310 8 49 316 6 954 281 399 113 202 974 590 99 78 89 590 612 280 740 990 147 348 750 341 401 71 695 396 376 4 113 542 224 221 83 825 880 969 233 343 824 860 28 998 808 399 122 536 305 928 439 278 70 419 842 379 766 342 779 651 20 64 479 259 514 206 712 543 983 647 865 956 760 790 316 822 528 589 375 230 132 624 580 921 610 542 287 626 185 707 496 743 697 240 876 901 441 931 864 9 387 350 600 846 591 42 384 730 435 147 975 912 370 296 520 173 372 985 75 469 350 630 932 352 303 969 542 915 715 457 111 384 915 208 527 453 175 9 995 69 979 963 854 838 763 592 546 366 14 297 353 343 187 729 769 488 77 701 556 842 385 148 434 886 805 534 628 734 678 854 491 160 801 999 247 261 656 820 65 986 133 228 927 870 700 160 332 674 271 619 473 988 880 933 670 102 743 223 177 121 241 195 956 970 573 226 329 955 708 607 628 78 801 906 494 311 497 280 363 718 98 395 494 994 646 781 697 788 671 831 679 70 637 374 597 578 213 243 951 489 635 900 739 397 729 400 578 377 171 251 553 823 555 522 924 582 705 500 312 345 515 829 631 804 376 301 134 226 448 539 659 265 306 572 863 715 552 13 51 842 426 546 137 283 491 971 52 47 391 190 430 664 170 22 413 505 990 460 385 831 934 972 30 763 892 387 98 27 510 701 253 365 486 638 86 836 547 936 341 713 772 482 355 51 521 460 997 770 897 836 270 672 578 788 879 574 439 15 416 132 4 957 39 994 756 588 300 494 952 963 212 59 109 931 22 731 694 855 69 263 573 542 82 367 612 48 747 285 748 989 263 640 758 708 208 787 114 235 144 304 331 205 236 126 934 869 30 475 889 597 575 992 287 23 112 670 672 687 602 576 211 105 724 409 227 760 8 725 481 973 610 379 119 491 877 298 390 94 694 231 404 577 106 687 145 546 164 331 379 353 952 971 666 219 58 352 105 812 475 572 664 174 584 260 279 694 363 251 8 133 45 648 691 964 273 216 489 917 888 758 227 359 652 918 539 801 18 653 378 958 836 44 463 375 458 525 694 173 300 954 670 457 459 766 822 303 187 118 946 630 748 819 697 620 675 484 795 89 36 763 628 278 37 660 689 468 640 434 261 641 514 230 25 196 642 358 197 629 192 534 842 935 26 757 656 682 292 621 318 168 425 497 456 127 227 373 651 798 609 179 924 285 603 74 252 312 688 25 687 947 754 635 444 773 63 312 145 665 201 892 635 341 952 410 675 632 623 738 471 506 464 913 829 942 945 135 894 579 245 762 737 989 129 487 391 677 338
k = 0
t = 0
The goal is to receive sorted array by growth like this: (example, used bubble sort to do it)
Array:
193 664 875 780 939 115 105 79 758 183 458 515 132 265 192 22 765 381 784 128 278 865 657 387 268 183 262 173 337 410 141 825 198 907 398 149 387 780 806 370 798 753 19 458 590 135 653 880 30 53 281 654 245 674 686 133 728 161 255 977 241 927 405 336 262 269 560 979 443 480 4 131 140 199 851 590 704 889 90 132 898 262 315 368 448 989 539 533 75 84 100 307 877 402 573 611 708 947 895 867 733 548 450 668 844 301 675 627 514 965 839 939 540 397 697 991 675 611 37 171 479 446 533 76 246 391 264 11 692 316 315 635 156 319 24 19 150 128 909 291 811 812 427 533 872 538 155 896 698 278 158 609 899 596 704 600 325 81 748 128 66 418 245 724 76 181 590 143 268 657 278 529 750 553 652 17 524 940 325 193 95 533 165 404 18 433 257 820 319 822 25 712 223 662 837 26 675 832 53 277 858 191 414 501 574 852 705 54 782 831 275 627 144 618 695 931 825 343 488 580 843 954 144 437 753 819 398 570 585 572 856 489 993 934 587 778 387 685 851 132 336 125 37 988 330 414 761 53 87 940 587 294 27 296 393 41 110 531 123 905 450 467 548 867 283 536 946 780 158 753 566 963 704 626 368 501 152 897 260 319 655 625 763 682 889 581 319 965 401 754 231 532 58 794 817 839 479 182 494 993 755 896 968 968 667 17 613 176 108 407 726 986 490 555 783 70 10 21 710 85 560 727 433 870 121 788 120 216 393 720 839 579 332 486 948 394 162 28 448 457 399 549 897 459 199 359 544 381 841 799 801 899 148 66 658 799 138 199 233 364 698 660 299 837 617 833 595 458 667 609 627 678 761 273 730 165 222 776 646 566 444 630 770 27 811 517 840 253 568 427 362 252 962 316 315 378 680 838 880 649 173 772 79 950 149 606 279 751 508 614 991 519 668 317 953 808 538 664 428 51 926 258 327 683 11 890 126 153 419 784 473 682 684 498 819 628 317 165 465 989 491 532 336 790 624 192 687 425 552 134 636 300 791 227 852 350 356 918 576 726 379 758 934 547 480 452 692 774 602 671 587 256 7 771 753 317 233 341 900 296 894 759 335 587 966 351 124 284 758 656 914 554 13 880 239 682 348 273 526 577 980 491 467 916 549 58 662 462 247 664 475 40 597 74 946 414 648 644 330 959 508 864 982 286 607 813 490 62 145 794 356 127 546 227 722 873 781 220 329 744 447 320 754 55 99 79 402 274 801 931 625 247 795 673 272 157 612 969 347 6 648 200 333 910 454 463 688 16 480 933 383 863 853 885 523 210 367 388 933 66 350 233 859 569 301 574 577 623 93 65 18 438 443 975 500 718 242 457 313 975 389 582 661 394 110 413 478 747 307 738 898 377 840 13 687 156 262 242 807 981 67 33 813 293 512 190 134 414 723 190 27 336 261 653 484 81 507 671 191 40 446 36 90 868 160 857 58 161 237 386 923 730 572 312 93 659 556 420 560 201 890 571 110 476 914 651 17 225 224 678 788 429 12 945 905 240 914 447 136 822 551 506 838 416 639 260 283 559 602 734 350 607 114 621 906 31 836 302 913 536 75 566 99 202 828 188 46 534 752 630 540 554 168 433 258 65 398 513 706 656 176 828 797 724 161 772 378 242 247 73 40 81 366 680 401 281 855 387 444 279 248 564 354 914 627 995 367 535 358 593 742 917 134 972 785 814 833 181 50 643 311 321 809 650 281 476 587 200 872 392 280 827 674 943 360 231 513 663 379 305 12 230 116 880 456 859 46 855 851 604 692 370 397 127 0 897 965 197 843 895 530 148 442 510 565 154 691 417 856 425 177 883 476 542 861 119 25 855 230 391 226 512 714 842 0 475 730 982 688 793 467 400 837 67 106 350 343 436 363 231 242 575 364 626 333 512 843 56 253 475 477 430 760 641 705 21 792 45 119 344 996 629 596 967 457 370 930 780 959 940 316 946 282 62 722 297 901 740 764 265 637 847 939 392 435 152 949 252 742 83 31 227 755 347 586 362 162 583 96 230 738 942 0 871 528 142 673 199 557 895 503 276 307 930 207 339 313 223 56 710 106 27 513 814 770 865 138 313 104 519 161 935 850 920 860 247 416 326 278 741 312 358 63 6 755 421 58 172 441 196 672 915 582 541 659 198 78 389 703 629 503 354 191 399 258 110 13 261 977 823 500 829 983 233 395 755 546 949 941 78 731 374 894 616 70 558 627 824 609 618 469 374 877 602 578 715 165 630 317 384
Sorted array:
0 0 0 4 6 6 7 10 11 11 12 12 13 13 13 16 17 17 17 18 18 19 19 21 21 22 24 25 25 26 27 27 27 27 28 30 31 31 33 36 37 37 40 40 40 41 45 46 46 50 51 53 53 53 54 55 56 56 58 58 58 58 62 62 63 65 65 66 66 66 67 67 70 70 73 74 75 75 76 76 78 78 79 79 79 81 81 81 83 84 85 87 90 90 93 93 95 96 99 99 100 104 105 106 106 108 110 110 110 110 114 115 116 119 119 120 121 123 124 125 126 127 127 128 128 128 131 132 132 132 133 134 134 134 135 136 138 138 140 141 142 143 144 144 145 148 148 149 149 150 152 152 153 154 155 156 156 157 158 158 160 161 161 161 161 162 162 165 165 165 165 168 171 172 173 173 176 176 177 181 181 182 183 183 188 190 190 191 191 191 192 192 193 193 196 197 198 198 199 199 199 199 200 200 201 202 207 210 216 220 222 223 223 224 225 226 227 227 227 230 230 230 231 231 231 233 233 233 233 237 239 240 241 242 242 242 242 245 245 246 247 247 247 247 248 252 252 253 253 255 256 257 258 258 258 260 260 261 261 262 262 262 262 264 265 265 268 268 269 272 273 273 274 275 276 277 278 278 278 278 279 279 280 281 281 281 282 283 283 284 286 291 293 294 296 296 297 299 300 301 301 302 305 307 307 307 311 312 312 313 313 313 315 315 315 316 316 316 317 317 317 317 319 319 319 319 320 321 325 325 326 327 329 330 330 332 333 333 335 336 336 336 336 337 339 341 343 343 344 347 347 348 350 350 350 350 351 354 354 356 356 358 358 359 360 362 362 363 364 364 366 367 367 368 368 370 370 370 374 374 377 378 378 379 379 381 381 383 384 386 387 387 387 387 388 389 389 391 391 392 392 393 393 394 394 395 397 397 398 398 398 399 399 400 401 401 402 402 404 405 407 410 413 414 414 414 414 416 416 417 418 419 420 421 425 425 427 427 428 429 430 433 433 433 435 436 437 438 441 442 443 443 444 444 446 446 447 447 448 448 450 450 452 454 456 457 457 457 458 458 458 459 462 463 465 467 467 467 469 473 475 475 475 476 476 476 477 478 479 479 480 480 480 484 486 488 489 490 490 491 491 494 498 500 500 501 501 503 503 506 507 508 508 510 512 512 512 513 513 513 514 515 517 519 519 523 524 526 528 529 530 531 532 532 533 533 533 533 534 535 536 536 538 538 539 540 540 541 542 544 546 546 547 548 548 549 549 551 552 553 554 554 555 556 557 558 559 560 560 560 564 565 566 566 566 568 569 570 571 572 572 573 574 574 575 576 577 577 578 579 580 581 582 582 583 585 586 587 587 587 587 587 590 590 590 593 595 596 596 597 600 602 602 602 604 606 607 607 609 609 609 611 611 612 613 614 616 617 618 618 621 623 624 625 625 626 626 627 627 627 627 627 628 629 629 630 630 630 635 636 637 639 641 643 644 646 648 648 649 650 651 652 653 653 654 655 656 656 657 657 658 659 659 660 661 662 662 663 664 664 664 667 667 668 668 671 671 672 673 673 674 674 675 675 675 678 678 680 680 682 682 682 683 684 685 686 687 687 688 688 691 692 692 692 695 697 698 698 703 704 704 704 705 705 706 708 710 710 712 714 715 718 720 722 722 723 724 724 726 726 727 728 730 730 730 731 733 734 738 738 740 741 742 742 744 747 748 750 751 752 753 753 753 753 754 754 755 755 755 755 758 758 758 759 760 761 761 763 764 765 770 770 771 772 772 774 776 778 780 780 780 780 781 782 783 784 784 785 788 788 790 791 792 793 794 794 795 797 798 799 799 801 801 806 807 808 809 811 811 812 813 813 814 814 817 819 819 820 822 822 823 824 825 825 827 828 828 829 831 832 833 833 836 837 837 837 838 838 839 839 839 840 840 841 842 843 843 843 844 847 850 851 851 851 852 852 853 855 855 855 856 856 857 858 859 859 860 861 863 864 865 865 867 867 868 870 871 872 872 873 875 877 877 880 880 880 880 883 885 889 889 890 890 894 894 895 895 895 896 896 897 897 897 898 898 899 899 900 901 905 905 906 907 909 910 913 914 914 914 914 915 916 917 918 920 923 926 927 930 930 931 931 933 933 934 934 935 939 939 939 940 940 940 941 942 943 945 946 946 946 947 948 949 949 950 953 954 959 959 962 963 965 965 965 966 967 968 968 969 972 975 975 977 977 979 980 981 982 982 983 986 988 989 989 991 991 993 993 995 996
k = 952047
t = 249349
I would highly appreciate your help!
When I'm trying to find mistakes, compiler says there are no ones
In general, most problems logical errors are not detected by compilers. There are more automated tools at your disposal though, such as compiling with -fsanitize=address (or similar compilation option, the exact spelling of which depends on which compiler you use). ASan can detect certain cases in which memory is accessed improperly, and it finds an issue in this code. But accessing memory improperly is not the only bug this code has.
Let's look at this piece of code:
int max = (a[0] / place) % 10;
for (int i = 1; i < n; i++)
{
k++;
if (((a[i] / place) % 10) > place)
{
max = a[i];
t++;
}
}
This looks like it's supposed to find the maximum digit, but it doesn't do that. ((a[i] / place) % 10) > place is the wrong comparison, and max = a[i] saves the wrong value. max could end up very large (potentially causing a stack overflow due causing a large count array to be allocated) or very small (causing the count array to be accessed out of bounds later).
This is unnecessary anyway, you can use int count[10]; and skip calculating max.
By the way t doesn't count swaps and I don't think that's even a relevant measurement to make in the context of radix sort - it's not based on swaps.
Let's look at this piece of code:
for (int i = 0; i < 10; i++)
{
count[i] += count[i-1];
}
So when i = 0, we access count[-1] (ASan detects this). That's bad, even if it "works", it risks pulling in some random value from whatever is before the array and adding it to every entry. Start this loop at 1.
Let's look at this piece of code:
radix(a, n);
Looks innocent.. but n was never assigned its proper value, and is still zero (being a global variable, it is initialized to zero). Most of the sorting code doesn't do anything, because it thinks the array has length 0.
It has been a while since I programmed in C but my first guess is that the int a[1000] parameter might copy the data all the time and that you need to pass the array by reference instead.
Investigated a little further:
the line if (((a[i] / place) % 10) > place) should be if (((a[i] / place) % 10) > max)
n is used uninitialised

How to send a keystroke with a LUA script?

How do you send a keystroke using a .lua script?
i.e. Trigger a F7 keypress (or any key).
My specific environment is Windows 64bit and running a lua script within OBS Studio(but I guess the environment doesn't matter).
I just started playing with this for one of my sons games and I came across this list. Use
keypress(101) would send the keypress "E"
Here is the list via raw pastebin link containing all codes and the corresponding keys.
https://pastebin.com/raw/h13kPdD3
Unknown
0
Backspace
8
Tab
9
Clear
12
Return
13
Pause
19
Escape
27
Space
32
QuotedDouble
34
Hash
35
Dollar
36
Percent
37
Ampersand
38
Quote
39
LeftParenthesis
40
RightParenthesis
41
Asterisk
42
Plus
43
Comma
44
Minus
45
Period
46
Slash
47
Zero
48
One
49
Two
50
Three
51
Four
52
Five
53
Six
54
Seven
55
Eight
56
Nine
57
Colon
58
Semicolon
59
LessThan
60
Equals
61
GreaterThan
62
Question
63
At
64
LeftBracket
91
BackSlash
92
RightBracket
93
Caret
94
Underscore
95
Backquote
96
A
97
B
98
C
99
D
100
E
101
F
102
G
103
H
104
I
105
J
106
K
107
L
108
M
109
N
110
O
111
P
112
Q
113
R
114
S
115
T
116
U
117
V
118
W
119
X
120
Y
121
Z
122
LeftCurly
123
Pipe
124
RightCurly
125
Tilde
126
Delete
127
KeypadZero
256
KeypadOne
257
KeypadTwo
258
KeypadThree
259
KeypadFour
260
KeypadFive
261
KeypadSix
262
KeypadSeven
263
KeypadEight
264
KeypadNine
265
KeypadPeriod
266
KeypadDivide
267
KeypadMultiply
268
KeypadMinus
269
KeypadPlus
270
KeypadEnter
271
KeypadEquals
272
Up
273
Down
274
Right
275
Left
276
Insert
277
Home
278
End
279
PageUp
280
PageDown
281
LeftShift
304
RightShift
303
LeftMeta
310
RightMeta
309
LeftAlt
308
RightAlt
307
LeftControl
306
RightControl
305
CapsLock
301
NumLock
300
ScrollLock
302
LeftSuper
311
RightSuper
312
Mode
313
Compose
314
Help
315
Print
316
SysReq
317
Break
318
Menu
319
Power
320
Euro
321
Undo
322
F1
282
F2
283
F3
284
F4
285
F5
286
F6
287
F7
288
F8
289
F9
290
F10
291
F11
292
F12
293
F13
294
F14
295
F15
296
World0
160
World1
161
World2
162
World3
163
World4
164
World5
165
World6
166
World7
167
World8
168
World9
169
World10
170
World11
171
World12
172
World13
173
World14
174
World15
175
World16
176
World17
177
World18
178
World19
179
World20
180
World21
181
World22
182
World23
183
World24
184
World25
185
World26
186
World27
187
World28
188
World29
189
World30
190
World31
191
World32
192
World33
193
World34
194
World35
195
World36
196
World37
197
World38
198
World39
199
World40
200
World41
201
World42
202
World43
203
World44
204
World45
205
World46
206
World47
207
World48
208
World49
209
World50
210
World51
211
World52
212
World53
213
World54
214
World55
215
World56
216
World57
217
World58
218
World59
219
World60
220
World61
221
World62
222
World63
223
World64
224
World65
225
World66
226
World67
227
World68
228
World69
229
World70
230
World71
231
World72
232
World73
233
World74
234
World75
235
World76
236
World77
237
World78
238
World79
239
World80
240
World81
241
World82
242
World83
243
World84
244
World85
245
World86
246
World87
247
World88
248
World89
249
World90
250
World91
251
World92
252
World93
253
World94
254
World95
255
ButtonX
1000
ButtonY
1001
ButtonA
1002
ButtonB
1003
ButtonR1
1004
ButtonL1
1005
ButtonR2
1006
ButtonL2
1007
ButtonR3
1008
ButtonL3
1009
ButtonStart
1010
ButtonSelect
1011
DPadLeft
1012
DPadRight
1013
DPadUp
1014
DPadDown
1015
Thumbstick1
1016
Thumbstick2
1017

Textbook C Code produces incorrrect output [duplicate]

This question already has answers here:
Convert int to double
(3 answers)
Closed 4 years ago.
I am learning C from a text. An example code provided by the author is:
#include <math.h>
main()
{ int i;
printf("\t Number \t\t Square Root of Number\n\n");
for (i=0; i<=360; ++i)
printf("\t %d \t\t\t %d \n",i, sqrt((double) i));
}
which on my computer produces an incorrect output I don't understand:
Number Square Root of Number
0 259
1 515
2 771
3 1027
4 1283
5 1539
6 1795
7 2051
8 2307
9 2563
10 2819
11 3075
12 3331
13 3587
14 3843
15 4099
16 4355
17 4611
18 4867
19 5123
20 5379
21 5635
22 5891
23 6147
24 6403
25 6659
26 6915
27 7171
28 7427
29 7683
30 7939
31 8195
32 8451
33 8707
34 8963
35 9219
36 9475
37 9731
38 9987
39 10243
40 10499
41 10755
42 11011
43 11267
44 11523
45 11779
46 12035
47 12291
48 12547
49 12803
50 13059
51 13315
52 13571
53 13827
54 14083
55 14339
56 14595
57 14851
58 15107
59 15363
60 15619
61 15875
62 16131
63 16387
64 16643
65 16899
66 17155
67 17411
68 17667
69 17923
70 18179
71 18435
72 18691
73 18947
74 19203
75 19459
76 19715
77 19971
78 20227
79 20483
80 20739
81 20995
82 21251
83 21507
84 21763
85 22019
86 22275
87 22531
88 22787
89 23043
90 23299
91 23555
92 23811
93 24067
94 24323
95 24579
96 24835
97 25091
98 25347
99 25603
100 25859
101 26115
102 26371
103 26627
104 26883
105 27139
106 27395
107 27651
108 27907
109 28163
110 28419
111 28675
112 28931
113 29187
114 29443
115 29699
116 29955
117 30211
118 30467
119 30723
120 30979
121 31235
122 31491
123 31747
124 32003
125 32259
126 32515
127 32771
128 33027
129 33283
130 33539
131 33795
132 34051
133 34307
134 34563
135 34819
136 35075
137 35331
138 35587
139 35843
140 36099
141 36355
142 36611
143 36867
144 37123
145 37379
146 37635
147 37891
148 38147
149 38403
150 38659
151 38915
152 39171
153 39427
154 39683
155 39939
156 40195
157 40451
158 40707
159 40963
160 41219
161 41475
162 41731
163 41987
164 42243
165 42499
166 42755
167 43011
168 43267
169 43523
170 43779
171 44035
172 44291
173 44547
174 44803
175 45059
176 45315
177 45571
178 45827
179 46083
180 46339
181 46595
182 46851
183 47107
184 47363
185 47619
186 47875
187 48131
188 48387
189 48643
190 48899
191 49155
192 49411
193 49667
194 49923
195 50179
196 50435
197 50691
198 50947
199 51203
200 51459
201 51715
202 51971
203 52227
204 52483
205 52739
206 52995
207 53251
208 53507
209 53763
210 54019
211 54275
212 54531
213 54787
214 55043
215 55299
216 55555
217 55811
218 56067
219 56323
220 56579
221 56835
222 57091
223 57347
224 57603
225 57859
226 58115
227 58371
228 58627
229 58883
230 59139
231 59395
232 59651
233 59907
234 60163
235 60419
236 60675
237 60931
238 61187
239 61443
240 61699
241 61955
242 62211
243 62467
244 62723
245 62979
246 63235
247 63491
248 63747
249 64003
250 64259
251 64515
252 64771
253 65027
254 65283
255 65539
256 65795
257 66051
258 66307
259 66563
260 66819
261 67075
262 67331
263 67587
264 67843
265 68099
266 68355
267 68611
268 68867
269 69123
270 69379
271 69635
272 69891
273 70147
274 70403
275 70659
276 70915
277 71171
278 71427
279 71683
280 71939
281 72195
282 72451
283 72707
284 72963
285 73219
286 73475
287 73731
288 73987
289 74243
290 74499
291 74755
292 75011
293 75267
294 75523
295 75779
296 76035
297 76291
298 76547
299 76803
300 77059
301 77315
302 77571
303 77827
304 78083
305 78339
306 78595
307 78851
308 79107
309 79363
310 79619
311 79875
312 80131
313 80387
314 80643
315 80899
316 81155
317 81411
318 81667
319 81923
320 82179
321 82435
322 82691
323 82947
324 83203
325 83459
326 83715
327 83971
328 84227
329 84483
330 84739
331 84995
332 85251
333 85507
334 85763
335 86019
336 86275
337 86531
338 86787
339 87043
340 87299
341 87555
342 87811
343 88067
344 88323
345 88579
346 88835
347 89091
348 89347
349 89603
350 89859
351 90115
352 90371
353 90627
354 90883
355 91139
356 91395
357 91651
358 91907
359 92163
360 92419
Any idea or clue as to why? If its obvious maybe point to a reference that will point out some stupid mistake I have made?
Read up on this: printf
And use %f for double not %d which is for int
You're using the wrong format specifier here:
printf("\t %d \t\t\t %d \n",i, sqrt((double) i));
Make sure to use %f for variables of type double (what a function like sqrt() returns):
printf("\t %d \t\t\t %f \n",i, sqrt((double) i));
You have used a wrong format specifier.
Try,
printf("\t %d \t\t\t %f \n" ,i , sqrt((double) i));
%f for double. Good luck.

Subroutine recursion in Perl

EDIT: I'm glad no one has spent any time pointing out that the actual text in line 6 and 7 has a different number than the input for their respective function calls. Eventually I'll be doing it for those two numbers (724 and 27), but for the sake of troubleshooting, I picked numbers with much smaller sequences. So, if anyone was wondering, that's why...
So, I've been learning Perl, and am relatively new to programming in general. My supervisor has a set of exercises for me to go through. The current one deals with Hailstone sequences, and she wants me to write a subroutine that prints the sequence for a given number.
The problem I'm running into is that, no matter what I've tried, if I call the function more than once, it will produce the sequence for the first number I call the function with, but the second time I call the function, it produces the sequence of the first call followed by the sequence of the second. So, this code:
#!usr/bin/perl
use strict;
use warnings;
print "\nThe hailstone sequence for 724 is:\n" . &hail(8) . "\n\n";
print "The hailstone sequence for 27 is:\n" . &hail(16) . "\n\n";
my $n;
my #seq;
sub hail {
no warnings 'recursion';
$n = $_[0];
if ($n > 1) {
push #seq, $n;
if ($n % 2 == 0) {
$n = $n/2;
} else {
$n = (3 * $n) + 1;
}
&hail($n);
} else {
push #seq, $n;
}
return "#seq";
}
produces:
The hailstone sequence for 724 is:
8 4 2 1
The hailstone sequence for 27 is:
8 4 2 1 16 8 4 2 1
I understand that this is most likely due to the fact that #seq doesn't get cleared out after each time the subroutine runs, but I've tried as many different ways as I can think of to clear it out so that each time I call the subroutine, it displays the sequence for -only- that number but they all either result in what I show here, or in showing nothing. How do I go about clearing the array each time?
Thanks very much.
You don't need recursion here. In my Fibonacci example in Mastering Perl, I show that it's easier to do it with iteration where you manage the queue yourself rather than using the call stack to do it.
Here's a general iterative solution that uses an array to keep track of the work left to do:
use strict;
use warnings;
use v5.10;
say "The hailstone sequence for 724 is:\n\t" .
join " ", hail(8);
say "The hailstone sequence for 27 is:\n\t" .
join " ", hail(16);
sub hail {
my #queue = ( $_[0] );
my #sequence = ();
while( my $next = shift #queue ) {
if( $next > 1 ) {
push #queue, do {
if( $next % 2 == 0 ) { $next / 2 }
else { 3*$next + 1 }
};
}
push #sequence, $next;
}
#sequence;
}
From there, I could add caching and other things so I can reuse sequences I've already generated (which works even without showing off some exciting new Perl features such as subroutine signatures and postfix dereferencing that I find quite fun):
use strict;
use warnings;
use v5.22;
use feature qw(signatures postderef);
no warnings qw(experimental::signatures experimental::postderef);
say "The hailstone sequence for 724 is:\n\t" .
join " ", hail(8)->#*;
say "The hailstone sequence for 27 is:\n\t" .
join " ", hail(16)->#*;
sub hail ( $n ) {
my #queue = ( $_[0] );
state $sequence = { 1 => [ 1 ] };
return $sequence->{$n} if exists $sequence->{$n};
my #sequence = ();
while( my $next = shift #queue ) {
say "Processing $next"; # to watch what happens
if( exists $sequence->{$next} ) {
push #sequence, $sequence->{$next}->#*;
next;
}
push #queue, do {
if( $next % 2 == 0 ) { $next / 2 }
else { 3*$next + 1 }
};
push #sequence, $next;
}
$sequence->{$n} = \#sequence;
}
I threw a say in there to show what I process. You can see that with 16, it doesn't have to go past 8 because it already knows that answer:
Processing 8
Processing 4
Processing 2
Processing 1
The hailstone sequence for 724 is:
8 4 2 1
Processing 16
Processing 8
The hailstone sequence for 27 is:
16 8 4 2 1
I was curious which numbers might cause a problem, so I slightly modified your example to return a list so I could easily count the number of elements. Several numbers produced sequences with over 100 numbers:
use strict;
use warnings;
use v5.10;
foreach my $n ( 0 .. 100 ) {
hail( $n, \my #seq );
say "$n [" . #seq . "] #seq";
}
sub hail {
my $n = $_[0];
my $s = $_[1];
if ($n > 1) {
push #$s, $n;
if ($n % 2 == 0) {
$n = $n/2;
} else {
$n = (3 * $n) + 1;
}
hail($n, $s);
} else {
push #$s, $n;
}
}
The output, without the deep recursion warnings (which should be the hint not to do it that way ;):
0 [1] 0
1 [1] 1
2 [2] 2 1
3 [8] 3 10 5 16 8 4 2 1
4 [3] 4 2 1
5 [6] 5 16 8 4 2 1
6 [9] 6 3 10 5 16 8 4 2 1
7 [17] 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
8 [4] 8 4 2 1
9 [20] 9 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
10 [7] 10 5 16 8 4 2 1
11 [15] 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
12 [10] 12 6 3 10 5 16 8 4 2 1
13 [10] 13 40 20 10 5 16 8 4 2 1
14 [18] 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
15 [18] 15 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
16 [5] 16 8 4 2 1
17 [13] 17 52 26 13 40 20 10 5 16 8 4 2 1
18 [21] 18 9 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
19 [21] 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
20 [8] 20 10 5 16 8 4 2 1
21 [8] 21 64 32 16 8 4 2 1
22 [16] 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
23 [16] 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
24 [11] 24 12 6 3 10 5 16 8 4 2 1
25 [24] 25 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
26 [11] 26 13 40 20 10 5 16 8 4 2 1
27 [112] 27 82 41 124 62 31 94 47 142 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
28 [19] 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
29 [19] 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
30 [19] 30 15 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
31 [107] 31 94 47 142 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
32 [6] 32 16 8 4 2 1
33 [27] 33 100 50 25 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
34 [14] 34 17 52 26 13 40 20 10 5 16 8 4 2 1
35 [14] 35 106 53 160 80 40 20 10 5 16 8 4 2 1
36 [22] 36 18 9 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
37 [22] 37 112 56 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
38 [22] 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
39 [35] 39 118 59 178 89 268 134 67 202 101 304 152 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
40 [9] 40 20 10 5 16 8 4 2 1
41 [110] 41 124 62 31 94 47 142 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
42 [9] 42 21 64 32 16 8 4 2 1
43 [30] 43 130 65 196 98 49 148 74 37 112 56 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
44 [17] 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
45 [17] 45 136 68 34 17 52 26 13 40 20 10 5 16 8 4 2 1
46 [17] 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
47 [105] 47 142 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
48 [12] 48 24 12 6 3 10 5 16 8 4 2 1
49 [25] 49 148 74 37 112 56 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
50 [25] 50 25 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
51 [25] 51 154 77 232 116 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
52 [12] 52 26 13 40 20 10 5 16 8 4 2 1
53 [12] 53 160 80 40 20 10 5 16 8 4 2 1
54 [113] 54 27 82 41 124 62 31 94 47 142 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
55 [113] 55 166 83 250 125 376 188 94 47 142 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
56 [20] 56 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
57 [33] 57 172 86 43 130 65 196 98 49 148 74 37 112 56 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
58 [20] 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
59 [33] 59 178 89 268 134 67 202 101 304 152 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
60 [20] 60 30 15 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
61 [20] 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
62 [108] 62 31 94 47 142 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
63 [108] 63 190 95 286 143 430 215 646 323 970 485 1456 728 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
64 [7] 64 32 16 8 4 2 1
65 [28] 65 196 98 49 148 74 37 112 56 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
66 [28] 66 33 100 50 25 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
67 [28] 67 202 101 304 152 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
68 [15] 68 34 17 52 26 13 40 20 10 5 16 8 4 2 1
69 [15] 69 208 104 52 26 13 40 20 10 5 16 8 4 2 1
70 [15] 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
71 [103] 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
72 [23] 72 36 18 9 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
73 [116] 73 220 110 55 166 83 250 125 376 188 94 47 142 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
74 [23] 74 37 112 56 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
75 [15] 75 226 113 340 170 85 256 128 64 32 16 8 4 2 1
76 [23] 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
77 [23] 77 232 116 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
78 [36] 78 39 118 59 178 89 268 134 67 202 101 304 152 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
79 [36] 79 238 119 358 179 538 269 808 404 202 101 304 152 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
80 [10] 80 40 20 10 5 16 8 4 2 1
81 [23] 81 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
82 [111] 82 41 124 62 31 94 47 142 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
83 [111] 83 250 125 376 188 94 47 142 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
84 [10] 84 42 21 64 32 16 8 4 2 1
85 [10] 85 256 128 64 32 16 8 4 2 1
86 [31] 86 43 130 65 196 98 49 148 74 37 112 56 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
87 [31] 87 262 131 394 197 592 296 148 74 37 112 56 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
88 [18] 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
89 [31] 89 268 134 67 202 101 304 152 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
90 [18] 90 45 136 68 34 17 52 26 13 40 20 10 5 16 8 4 2 1
91 [93] 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
92 [18] 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
93 [18] 93 280 140 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
94 [106] 94 47 142 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
95 [106] 95 286 143 430 215 646 323 970 485 1456 728 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
96 [13] 96 48 24 12 6 3 10 5 16 8 4 2 1
97 [119] 97 292 146 73 220 110 55 166 83 250 125 376 188 94 47 142 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
98 [26] 98 49 148 74 37 112 56 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
99 [26] 99 298 149 448 224 112 56 28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
100 [26] 100 50 25 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
Should you want a true function — one that doesn't have any side-effects, and thus none of the problems you are having — it would look as follows:
sub hail {
no warnings qw( recursion );
my ($n) = #_;
if ($n > 1) {
if ($n % 2 == 0) {
return $n, hail($n/2);
} else {
return $n, hail(3*$n + 1);
}
} else {
return $n;
}
}
Note that recursion is totally unneeded here, greatly slowing down your program and increasing its memory footprint. The following is such an iterative solution:
sub hail {
my ($n) = #_;
my #rv;
while (1) {
push #rv, $n;
last if $n <= 1;
if ($n % 2 == 0) {
$n = $n/2;
} else {
$n = 3*$n + 1;
}
}
return #rv;
}
A question about the above code was asked in the comments. The following is the answer:
last jumps to the statement after the loop.
Another programmer might have written the following:
push #rv, $n;
while ($n > 1) {
...;
push #rv, $n;
}
But that contains duplicated code. Ideally, one seeks to avoid duplicated code. But since
while (EXPR) { STATEMENTS }
can be rewritten as
while (1) { last if !EXPR; STATEMENTS }
and since
push #rv, $n;
while ($n > 1) {
...;
push #rv, $n;
}
can be rewritten as
push #rv, $n;
while (1) {
last if $n <= 1;
...;
push #rv, $n;
}
we can remove the duplicated code as follows:
while (1) {
push #rv, $n;
last if $n <= 1;
...;
}
Not sure about perl, but in other languages I'd do
sub hail {
my #seq;
return hailRecursive(\#seq, $_[0])
}
And then implement hailRecursive in terms of array ref and $n
So, the full assignment was to print the sequence for any number < 100000 of my choosing, print the sequence of 27, and then find the number < 100000 with the longest sequence, and print the number of elements in the sequence (but not the sequence itself) I greatly appreciate the help everyone has provided about making a more effective subroutine, and I really will go through the different suggestions to learn the different tips and tricks from each one. I didn't modify my main subroutine code too much to keep it more in my (current) style for the sake of the exercise. (My supervisor won't care that I got help, but it still feels...-something-...to just copy somebody else's code without also knowing I could figure it out with my own hair-brained ideas, along with -her- suggestion to use a recursive subroutine, as well).
I took one person's advice to not return the array as a string, which helped with the part of the exercise I didn't originally mention, but then I had to rework how I printed the actual sequences which seemed easy enough. Beyond learning more effective subroutines, my main concern is still with clearing the array. Someone suggested just putting #seq = () after each instance, and that works. What I want to know is why what I actually have running in the code (the lines with ##### -after- the code) works to clear the array each time, but why it doesn't work to simply clear out the array after I return it in the subroutine, like I have with the commented out line. That still aggregates the sequence each time the subroutine is called.
#!usr/bin/perl
use strict;
use warnings;
my $num_win = 0;
my $elem_win = 0;
my #seq;
my $elements;
print "\nThe hailstone sequence for 724 is:\n";
&sequence(&hail(724));
#seq = (); #####
print "\n\nThe hailstone sequence for 27 is:\n";
&sequence(&hail(27));
#seq = (); #####
for (my $i=1; $i<100000; $i++) {
$elements = &hail($i);
if ($elements > $elem_win) {
$elem_win = $elements;
$num_win = $i;
}
#seq = (); #####
}
print "\n\nThe number with the largest sequence is: $num_win\n";
print "The number of elements in $num_win is: $elem_win\n\n";
my $n;
sub hail {
no warnings 'recursion';
$n = $_[0];
if ($n > 1) {
push #seq, $n;
if ($n % 2 == 0) {
$n = $n/2;
} else {
$n = (3 * $n) + 1;
}
&hail($n);
} else {
push #seq, $n;
}
return #seq;
##### #seq = ();
}
sub sequence {
my #hail_seq = #_;
foreach (#hail_seq) {
my $number = $_;
print "$number, ";
}
}
with the results:
The hailstone sequence for 724 is:
724, 362, 181, 544, 272, 136, 68, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1,
The hailstone sequence for 27 is:
27, 82, 41, 124, 62, 31, 94, 47, 142, 71, 214, 107, 322, 161, 484, 242, 121, 364, 182, 91, 274, 137, 412, 206, 103, 310, 155, 466, 233, 700, 350, 175, 526, 263, 790, 395, 1186, 593, 1780, 890, 445, 1336, 668, 334, 167, 502, 251, 754, 377, 1132, 566, 283, 850, 425, 1276, 638, 319, 958, 479, 1438, 719, 2158, 1079, 3238, 1619, 4858, 2429, 7288, 3644, 1822, 911, 2734, 1367, 4102, 2051, 6154, 3077, 9232, 4616, 2308, 1154, 577, 1732, 866, 433, 1300, 650, 325, 976, 488, 244, 122, 61, 184, 92, 46, 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1,
The number with the largest sequence is: 77031
The number of elements in 77031 is: 351
Change your 2nd else block to this and remove the return statement after that block.
else {
push #seq, $n;
my $seq = "#seq";
undef #seq;
return $seq;
}
You just need to initialize the array #seq before calling the hail subroutine a second time. Try this ...
my #seq;
print "\nThe hailstone sequence for 724 is:\n" . &hail(8) . "\n\n";
#seq = ();
print "The hailstone sequence for 27 is:\n" . &hail(16) . "\n\n";
my $n;
Similar to what others have posted.
sub hailstone {
my $n = shift;
return if $n == 1;
if ( $n % 2 == 0 ){
$n = $n / 2;
} else {
$n = ( $n * 3 ) + 1;
}
return $n."\n",hailstone($n)
}
Upon calling the subroutine:
say hailstone(5);
16
8
4
2
1

Adjusting Output (Wordwrap maybe?) [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
My output needs to look like this :
There are 8 prime numbers less than or equal to 19
2 3 5 7 11 13 17 19
There are 95 prime numbers less than or equal to 500
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71
73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173
179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281
283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409
419 421 431 433 439 443 449 457 461 463 467 479 487 491 499
There are 239 prime numbers less than or equal to 1500
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71
73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173
179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281
283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409
419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541
547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659
661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809
811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941
947 953 967 971 977 983 991 997 1009 1013 1019 1021 1031 1033 1039 1049 1051 1061 1063 1069
1087 1091 1093 1097 1103 1109 1117 1123 1129 1151 1153 1163 1171 1181 1187 1193 1201 1213 1217 1223
1229 1231 1237 1249 1259 1277 1279 1283 1289 1291 1297 1301 1303 1307 1319 1321 1327 1361 1367 1373
1381 1399 1409 1423 1427 1429 1433 1439 1447 1451 1453 1459 1471 1481 1483 1487 1489 1493 1499
Instead it looks like this:
There are 8 prime numbers less than or equal to 19
2 3 5 7 11 13 17 19
There are 95 prime numbers less than or equal to 500
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499
There are 239 prime numbers less than or equal to 1500
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997 1009 1013 1019 1021 1031 1033 1039 1049 1051 1061 1063 1069 1087 1091 1093 1097 1103 1109 1117 1123 1129 1151 1153 1163 1171 1181 1187 1193 1201 1213 1217 1223 1229 1231 1237 1249 1259 1277 1279 1283 1289 1291 1297 1301 1303 1307 1319 1321 1327 1361 1367 1373 1381 1399 1409 1423 1427 1429 1433 1439 1447 1451 1453 1459 1471 1481 1483 1487 1489 1493 1499
How can I fix this.
Code for this:
void writeToOutputFile(FILE *fpout, const int *array, int n, int count){
int i;
fprintf(fpout, "\n \nThere are %d prime numbers less than or equal to %d \n \n", count, n);
for(i = 0; i < count; i++){
if(*(array + i) != 0){
fprintf(fpout, "%d ", *(array + i));
}
}
}
iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
printf returns the number of characters printed onto the screen. You can use this information to print newlines when you reach a certain limit:
int width = 0;
for (/* some loop */) {
width += printf(/*some numbers */);
if (width > 70) {
printf("\n");
width = 0;
}
}
within your function:
void writeToOutputFile(FILE *fpout, const int *array, int n, int count){
int i;
int width = 0;
fprintf(fpout, "\n \nThere are %d prime numbers less than or equal to %d \n \n", count, n);
for(i = 0; i < count; i++){
if(*(array + i) != 0){
width += fprintf(fpout, "%d ", *(array + i));
if (width > 70) {
fprintf(fpout, "\n");
width = 0;
}
}
}
}

Resources