PowerShell: 77 74 71 70 61
Golfed code:
for($i=(read-host);$i-ne1;$x++){$i=(($i/2),(3*$i+1))[$i%2]}$x
Notes:
I originally tried taking the user input without forcing it to an integer, but that broke in an interesting way. Any odd inputs would process inaccurately, but even inputs would work fine. It took me a minute to realize what was going on.
When performing multiplication or addition, PowerShell treats un-typed input as a string first. So, '5'*3+1
becomes '5551' instead of 16. The even inputs behaved fine because PowerShell doesn't have a default action for division against strings. Even the even inputs which would progress through odd numbers worked fine because, by the time PowerShell got to an odd number in the loop, the variable was already forced to an integer by the math operations anyway.
Thanks to Danko Durbic for pointing out I could just invert the multiplication operation, and not have to cast read-host
to int since PowerShell bases its operations on the first object.
PowerShell Golfer's Tip: For some scenarios, like this one, switch
beats if/else
. Here, the difference was 2 characters.
Protip courtesy of Danko Durbic: For this particular scenario, an array can be used instead of switch
, to save 8 more characters!
There's no error checking for non-integer values, or integers less than two.
If you'd like to audit the script, put ;$i
just before the last close brace in the script.
I'm not sure exactly how well PowerShell handles numbers that progress into very large values, but I expect accuracy is lost at some point. Unfortunately, I also expect there's not much that can be done about that without seriously bloating the script.
Ungolfed code, with comments:
# Start for loop to run Collatz algorithm.
# Store user input in $i.
# Run until $i reaches 1.
# Increment a counter, $x, with each run.
for($i=(read-host);$i-ne1;$x++)
{
# New $i is defined based on an array element derived from old $i.
$i=(
# Array element 0 is the even numbers operation.
($i/2),
# Array element 1 is the odd numbers operation.
(3*$i+1)
# Array element that defines the new $i is selected by $i%2.
)[$i%2]
}
# Output $x when the loop is done.
$x
# Variable cleanup. Don't include in golfed code.
rv x,i
Test cases:
Below are some samples with auditing enabled. I've also edited the output some for clarity, by adding labels to the input and final count and putting in spacing to set apart the Collatz values.
---
Input: 2
1
Steps: 1
---
Input: 16
8
4
2
1
Steps: 4
---
Input: 5
16
8
4
2
1
Steps: 5
---
Input: 7
22
11
34
17
52
26
13
40
20
10
5
16
8
4
2
1
Steps: 16
---
Input: 42
21
64
32
16
8
4
2
1
Steps: 8
---
Input: 14
7
22
11
34
17
52
26
13
40
20
10
5
16
8
4
2
1
Steps: 17
---
Input: 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
Steps: 26
---
Input: 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
Steps: 106
---
Input: 6174
3087
9262
4631
13894
6947
20842
10421
31264
15632
7816
3908
1954
977
2932
1466
733
2200
1100
550
275
826
413
1240
620
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
Steps: 111
---
Input: 8008135
24024406
12012203
36036610
18018305
54054916
27027458
13513729
40541188
20270594
10135297
30405892
15202946
7601473
22804420
11402210
5701105
17103316
8551658
4275829
12827488
6413744
3206872
1603436
801718
400859
1202578
601289
1803868
901934
450967
1352902
676451
2029354
1014677
3044032
1522016
761008
380504
190252
95126
47563
142690
71345
214036
107018
53509
160528
80264
40132
20066
10033
30100
15050
7525
22576
11288
5644
2822
1411
4234
2117
6352
3176
1588
794
397
1192
596
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
Steps: 93
---
Interesting bits about the input numbers which are not from the question's test cases:
3
1+
is special-cased as)
. – Peter Taylor – 2013-08-01T11:59:31.637@PeterTaylor of course, forgot about that ;) – Volatility – 2013-08-01T12:01:33.433
There are a couple of replacements which tie for length and might be of interest. One is to replace the loop body with
.5*)).1&*+2/
; the other is to work withx-1
rather thanx
which turns the unfold into({}{.2%!{)6*}*2/}/
– Peter Taylor – 2013-08-01T17:22:07.8671Nice work! <!-- padding --> – Peter Taylor – 2013-08-02T08:20:14.717
1
@Peter: The <!-- --> don't work in comments. Use this instead.
– Ilmari Karonen – 2013-08-02T10:36:48.7332Or this. – Timwi – 2013-08-03T08:45:07.930
Well, I doubt anyone can get any shorter than this, so I'm accepting it. Good job! – Doorknob – 2013-08-04T01:42:50.473