Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions src/main/java/org/fundacionjala/coding/abel/SumFct.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.fundacionjala.coding.abel;

import java.math.BigInteger;

/**
* 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:
* #Hint: See Fibonacci sequence
* #Ref: http://oeis.org/A000045
* 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.
*/
public final class SumFct {

/**
* Private Constructor.
*/
private SumFct() {

}

/**
* Calculates the permiter of a rectangle compose by squares.
*
* @param n Number of rectanbles.
* @return Big Integer.
*/
public static BigInteger perimeter(BigInteger n) {
BigInteger a;
BigInteger b = BigInteger.ONE;
BigInteger c = BigInteger.ONE;
BigInteger total = BigInteger.ZERO;

for (int i = 0; i <= n.intValue(); i++) {
a = b;
b = c;
c = a.add(b);
total = total.add(a);
}

return total.multiply(BigInteger.valueOf(4));
}
}
59 changes: 59 additions & 0 deletions src/test/java/org/fundacionjala/coding/abel/SumFctTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package org.fundacionjala.coding.abel;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.math.BigInteger;

import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/**
* Created by abelb on 8/23/2017.
*/
public class SumFctTest {

/**
* Test for Private Constructors.
*
* @throws NoSuchMethodException Thrown when a particular method cannot be found.
* @throws IllegalAccessException Thrown when an application tries to reflectively create an instance.
* @throws InvocationTargetException thrown by an invoked method or constructor.
* @throws InstantiationException Thrown when an application tries to create an instance of a class
* using the {@code newInstance} method in class.
*/
@Test
public void privateConstructorTest() throws NoSuchMethodException, IllegalAccessException,
InvocationTargetException, InstantiationException {
Constructor<SumFct> constructor = SumFct.class.getDeclaredConstructor();
assertTrue(Modifier.isPrivate(constructor.getModifiers()));
constructor.setAccessible(true);
constructor.newInstance();
}

/**
* Provided test.
*/
@Test
public void test1() {
assertEquals(BigInteger.valueOf(80), SumFct.perimeter(BigInteger.valueOf(5)));
}

/**
* Provided test.
*/
@Test
public void test2() {
assertEquals(BigInteger.valueOf(216), SumFct.perimeter(BigInteger.valueOf(7)));
}

/**
* Provided test.
*/
@Test
public void test3() {
assertEquals(BigInteger.valueOf(14098308), SumFct.perimeter(BigInteger.valueOf(30)));
}
}