DEV Community

Vibhor Singh
Vibhor Singh

Posted on

Perimeters of squares in a rectangle

In the following image (you should zoom in if the screenshot isn't clearer to you), you can see the problem description and what the input and output are supposed to be.

Problem description

The input would be an integer and you have to calculate the sum of the perimeters of all the squares in a rectangle when there are n + 1 squares disposed in the same manner as shown in the following image -

Fibonacci Square Example screenshot

Above image has 6 squares which are fitted in a very unique manner. The sides of squares seem random at first glance but in reality, it is something that most noob coders also know - Fibonacci Sequence.

Notice the side of innermost circle is 1 and the progressing squares' sides are 1, 2, 3, 5, 8.

1, 1, 2, 3, 5, 8

In case you don't know, what Fibonacci sequence is - The Fibonacci sequence is the sequence in which each number is the sum of the two preceding ones. - Wikipedia

Approach:

  • Create a function which generates sum of 'n+1' Fibonacci numbers where 'n' is the input
  • Multiply it by 4 to calculate the perimeter of resulting sum

Algorithm:

  • The following function calculates the sum of 'n+1' fibonacci numbers
function fib(num) {
  let a=1;
  let b=1;
  let s=0;
  if(num===0) 
    return 1;
  while(num>1) {
    let c=a+b;
    s+=c;
    a=b;
    b=c;
    num-=1;
  }
  return s+2;
} 
Enter fullscreen mode Exit fullscreen mode

This function takes a number 'num' as an input;

  • The first two lines of 'fib()' function initializes and assign the first two Fibonacci numbers;
let a=1;
let b=1;
Enter fullscreen mode Exit fullscreen mode
  • Line 3 mentions 's' variable which stores the sum of 'num+1' Fibonacci numbers; let s=0;
  • Line 4-5: When input is 0, we return 1 as for input '0', num+1 number would be 1;
if(num===0) 
    return 1;
Enter fullscreen mode Exit fullscreen mode
  • Line 6-12: This block mentions a 'while()' loop as it would run until we add the required Fibonacci numbers which would Fibonacci sequence - 2 as the loop would run for num-2 times. Are you confused, don't be? I have noob coding writing skills :)
while(num>1) {
    let c=a+b;
    s+=c;
    a=b;
    b=c;
    num-=1;
  }
Enter fullscreen mode Exit fullscreen mode

The loop would run num-2 times as we have already initialized and declared the first two numbers of Fibonacci sequence mentioned in line 1-2;

  • We would return s+2 in last line of 'fib()' function because we ignored the first two numbers of Fibonacci sequence and calulated the sum of only the remaining Fibonacci numbers in 'while' loop;
    return s+2;

  • Now in the end, print the perimeter of sum of 'n+1' squares by multiplying 4 to the output of 'fib()' function as the output on the console/prompt screen.
    console.log( 4*fib(n));

This problem uses the concept of fibonacci numbers generation and a little bit of basic maths (e.g. formula for perimeter of a square).

Top comments (0)