Here you can find the source of powerOf31(int n)
static int powerOf31(int n)
//package com.java2s; /**/* ww w. j av a2s .c om*/ * Copyright (c) 2013 Eclipse contributors and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html */ public class Main { /** * A cached array for the 31^n. */ private static volatile int[] POWERS_OF_31 = new int[0]; /** * Returns 31^n. */ static int powerOf31(int n) { // Look up the result in the array. // return POWERS_OF_31.length <= n ? synchronizedPowerOf31(n) : POWERS_OF_31[n]; } private static synchronized int synchronizedPowerOf31(int n) { // Check again now that we've synchronized. // if (POWERS_OF_31.length <= n) { // Create a larger array. // int[] result = new int[Math.max(n + 100, 200)]; // Compute the power values. // int powerOf31 = 1; for (int i = 0; i < result.length; ++i) { result[i] = powerOf31; powerOf31 *= 31; } // Cache the result. // POWERS_OF_31 = result; } return POWERS_OF_31[n]; } }