Showing posts with label コラッツの問題. Show all posts
Showing posts with label コラッツの問題. Show all posts

Friday, July 1, 2016

Bash: Collatz Conjecture

Command:

$ cat ./collatz.sh

Result:

#!/bin/bash

collatz () {
        echo $1

        if (($1 != 1)) ; then
                if (($1 % 2 == 0)) ; then

                        collatz $(($1 / 2))
                else
                        collatz $((3 * $1 + 1))
                fi
        fi
}

collatz $1


Command:

$ ./collatz.sh 123

Result:

123
370
185
556
278
139
418
209
628
314
157
472
236
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


Collatz conjecture: The Simplest Impossible Problem