DEV Community

loading...

Discussion on: Coding Puzzles: Week of 4/8

Collapse
aspittel profile image
Ali Spittel Author

Thursday (5 KYU): Perimeter of squares in a rectangle

codewars.com/kata/559a28007caad2ac...

Collapse
choroba profile image
E. Choroba
#!/usr/bin/perl
use warnings;
use strict;

sub perimeters {
    my ($n) = @_;
    my @f = (1, 1);
    my $s = 0;
    for (0 .. $n) {
        $s += $f[0];
        @f = ($f[1], $f[0] + $f[1]);
    }
    return 4 * $s
}

# In a good TDD tradition, I started with these lines:
use Test::More tests => 2;
is perimeters(5), 80;
is perimeters(7), 216;
Collapse
clandau profile image
Courtney

TypeScript. Great practice!

export class G964 {
    public static perimeter = (n) => {
      let fibArray : number[] = [0, 1, 1];
      for(let i=3; i<=n+1; i++) {
        fibArray[i] = fibArray[i-1] + fibArray[i-2];
      }
      return fibArray.reduce((a, b) => a + b) * 4;
    }
}