2017年4月23日日曜日

開発環境

Think Perl 6: How to Think Like a Computer Scientist (Laurent Rosenfeld(著)、Allen B. Downey(著)、Oreilly & Associates Inc)のPart 1(Starting with the basics)、Chapter 4(Conditionals and Recursion)のExercises 4-4.を取り組んでみる。

Exercises 4-4.

コード(Emacs)

#!/usr/bin/env perl6
# -*- coding: utf-8 -*-

say '4-1.';

my $a = 1;
my $b = 1;
say 1;
say 1;
for 3..20 {
    say $a + $b;
    my $t = $a + $b;
    $a = $b;
    $b = $t;
}

say '4-2.';
my $n = Int(get);
my $result;

$a = 1;
$b = 1;
for 1..$n {
    if $_ <= 2 {
        $result = 1;
    } else {
        $result = $a + $b;
        $a = $b;
        $b = $result;
    }
}
say $result;

入出力結果(Terminal, REPL)

$ cat sample4.txt 
20
$ ./sample4.pl < sample4.txt 
4-1.
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765
4-2.
6765
$

0 コメント:

コメントを投稿