/* ============================================================================ * File: Series1.java * ---------------------------------------------------------------------------- * * This file is part of the Microsoft Supplemental UI Library for Visual J# .NET * Code Samples. * * Copyright (C) 2003 Microsoft Corporation. All rights reserved. * * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, * WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. * ============================================================================ */ package RecursionDemo; /** * This class implements RecursiveFunction interface to demonstrate calculation of * a series where nth number = n ^ 2 + (n - 1)th number as recursive funtion. * Refer Factorial.java also. */ public class Series1 implements RecursiveFunction { private RecursionStack stack; public String getName() { return "Number Series 1"; } public String getDescription() { return "This function generates number in series 1, 5, 14, 30, 55, 91, ..." + " i.e. nth number = n ^ 2 + (n - 1)th number."; } public String getCode() { return "public int series(int n) {\n" + " if (n <= 1)\n" + " return 1;\n" + " else\n" + " return (n * n + series(n - 1));\n" + "}"; } public int doRecursion(int n) { stack.push(n); int retVal; if (n <= 1) retVal = 1; else { retVal = (n * n) + doRecursion(n - 1); } stack.pop(retVal); return retVal; } public void setRecursionStack(RecursionStack s) { stack = s; } }