import org.junit.Test; import static junit.framework.TestCase.assertEquals; /** * Generic test class for implementing and testing a sequence in recursive and iterative form * _ _ _ _ * __ ___ __(_) |_| |_ ___ _ __ | |__ _ _ * \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | | * \ V V /| | | | |_| || __/ | | | | |_) | |_| | * \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, | * |___/ * ____ __ __ _ * / ___|_ _____ _ __ \ \ / /__ __ _ ___| | * \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ | * ___) \ V / __/ | | | \ V / (_) | (_| | __/ | * |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_| * |___/ * Licensed under the GPLv2 License, Version 2.0 (the "License"); * Copyright (c) Sven Vogel */ public class Aufgabe5 { /** * Calculate some recursive sequence * @param n the number to calculate the sequence of * @return the value for the given number of the sequence */ private static int recursiveFunction(int n) { if (n <= 0) { return 1; } else if (n == 1) { return 2; } return 4 * recursiveFunction(n - 2) - 1; } /** * Calculate some iterative sequence * @param n the number to calculate the sequence of * @return the value for the given number of the sequence */ private static int iterativeFunction(int n) { if (n <= 0) { return 1; } else if (n == 1) { return 2; } int sum = 1; for (int i = 2; i < n; i += 2) { sum = sum * 4 - 1; } return sum; } @Test public void test() { assertEquals(recursiveFunction(0), 1); assertEquals(recursiveFunction(1), 2); System.out.println("greater than 1: " + recursiveFunction(12)); assertEquals(recursiveFunction(67), iterativeFunction(67)); } }