The drawing shows 6 squares the sides of which have a length of 1, 1, 2, 3, 5, 8. It’s easy to see that the sum of the perimeters of these squares is : 4 * (1 + 1 + 2 + 3 + 5 + 8) = 4 * 20 = 80
Could you give the sum of the perimeters of all the squares in a rectangle when there are n + 1 squares disposed in the same manner as in the drawing:
The function perimeter has for parameter n where n + 1 is the number of squares (they are numbered from 0 to n) and returns the total perimeter of all the squares.
import java.math.BigInteger;
public class SumFct {
public static BigInteger perimeter(BigInteger n) {
BigInteger a = BigInteger.ZERO;
BigInteger b = BigInteger.ONE;
BigInteger c = BigInteger.ONE;
BigInteger sum = BigInteger.ZERO;
for(int i = 0; i <= n.intValue(); i++) {
a = b;
b = c;
c = a.add(b);
sum = sum.add(a);
}
return sum.multiply(BigInteger.valueOf(4));
}
}
import java.math.BigInteger;
public class SumFct {
public static BigInteger perimeter(BigInteger n) {
BigInteger a = BigInteger.ZERO;
BigInteger b = BigInteger.ONE;
BigInteger c = BigInteger.ONE;
BigInteger sum = BigInteger.ZERO;
for(int i = 0; i <= n.intValue(); i++) {
a = b;
b = c;
c = a.add(b);
sum = sum.add(a);
}
return sum.multiply(BigInteger.valueOf(4));
}
}