From 019cae70f21ea5cbaff967755fa47d3954912b74 Mon Sep 17 00:00:00 2001 From: Abel Barrientos Date: Wed, 23 Aug 2017 10:16:17 -0400 Subject: [PATCH] Solving the kata --- .../org/fundacionjala/coding/abel/SumFct.java | 45 ++++++++++++++ .../fundacionjala/coding/abel/SumFctTest.java | 59 +++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 src/main/java/org/fundacionjala/coding/abel/SumFct.java create mode 100644 src/test/java/org/fundacionjala/coding/abel/SumFctTest.java diff --git a/src/main/java/org/fundacionjala/coding/abel/SumFct.java b/src/main/java/org/fundacionjala/coding/abel/SumFct.java new file mode 100644 index 0000000..c1dfaaf --- /dev/null +++ b/src/main/java/org/fundacionjala/coding/abel/SumFct.java @@ -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)); + } +} diff --git a/src/test/java/org/fundacionjala/coding/abel/SumFctTest.java b/src/test/java/org/fundacionjala/coding/abel/SumFctTest.java new file mode 100644 index 0000000..19575af --- /dev/null +++ b/src/test/java/org/fundacionjala/coding/abel/SumFctTest.java @@ -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 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))); + } +}