Java Number Power powerOf31(int n)

Here you can find the source of powerOf31(int n)

Description

Returns 31^n.

License

Open Source License

Declaration

static int powerOf31(int n) 

Method Source Code

//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];
    }
}

Related

  1. powerMean(final double p, final double[] x)
  2. powerMod(long base, long exp, long mod)
  3. powerOf(final int value, final int powerOf)
  4. powerOf10(int number)
  5. powerOf2Log2(int n)
  6. powerOfN(Double v, int n)
  7. powerOfTen(int exp)
  8. powerOfTwo(final int i)
  9. powerOfTwo(int current, int desired)