Instantly share code, notes, and snippets.

# mohamed-ennahdi/Equation.java Last active Oct 9, 2015

 package com.blogspot.mohalgorithmsorbit.equation; /** * * @author Mohamed Ennahdi El Idrissi * */ public class Equation { int n; int base; private final double CEILINGED; private final double FLOORED; public Equation(int n, int base) throws Exception { if (base <= 1) { throw new Exception("Logarithm function cannot accept this base ("+base+") in this context."); } if (n <= 0) { throw new Exception("Logarithm function cannot accept the value of 'n'."); } this.n = n; this.base = base; CEILINGED = Math.ceil ( Math.log(n) / Math.log( base ) ); FLOORED = Math.floor( Math.log(n) / Math.log( base ) ); } protected double f() { return base * (FLOORED * Math.pow( this.base, 2 * FLOORED + 2) - FLOORED * Math.pow( base, 2 * FLOORED) - Math.pow( base, 2 * FLOORED + 2 ) + Math.pow( base, 2 ) ) / ( ( base - 1 ) * Math.pow( base + 1, 2 ) ) + ( ( n - Math.pow( base, FLOORED ) + 1 ) * FLOORED * Math.pow( base, CEILINGED ) ); } protected double g() { return ((FLOORED - 1) * Math.pow(base, 2 * FLOORED + 2 ) - FLOORED * Math.pow(base, 2 *FLOORED) + base * base)/((base - 1)*(base + 1)*(base + 1)) + ((n - Math.pow(base, FLOORED) + 1) * FLOORED * Math.pow(base, FLOORED)); } protected double h() { return ( ( n - Math.pow(base, FLOORED) + 1 ) * Math.pow( base, FLOORED ) ) + ( Math.pow( base, 2 * FLOORED) - 1 ) / ( base + 1 ); } protected double u() { return ( base * (FLOORED * Math.pow(base, FLOORED + 1) - (FLOORED + 1) * Math.pow(base, FLOORED) + 1 ) ) / ( Math.pow( base - 1, 2 ) ) + ( -1 * (FLOORED + 1) * Math.pow(base, 2* FLOORED) - (FLOORED +2)*Math.pow(base, FLOORED + 1) + (FLOORED - 1)*Math.pow(base, FLOORED + 2) + FLOORED * Math.pow(base, FLOORED + 3) + (FLOORED + 1)*Math.pow(base, 2* FLOORED + 1) + FLOORED * Math.pow(base, 2* FLOORED + 2) - FLOORED * Math.pow( base, 2 * FLOORED + 3) - (FLOORED + 1) * Math.pow(base, FLOORED) + (base * base) + base + 2) / ( -1 * Math.pow( ( base*base ) - 1, 2 ) ) + (n - Math.pow(base, FLOORED)) * CEILINGED * Math.pow(base, FLOORED); } protected double v() { return ( (-1 - FLOORED) * Math.pow( base, 2 * FLOORED ) - Math.pow( base, FLOORED + 1 ) + FLOORED * Math.pow( base, FLOORED + 2) + FLOORED * Math.pow( base, 2 * FLOORED + 2 ) - ( FLOORED + 1 ) * Math.pow( base, FLOORED ) + base + 2) / ( 2 * ( base * base - 1)) + ( ( n - Math.pow( base, FLOORED ) ) * CEILINGED * ( Math.pow( base, FLOORED ) + n + 1 ) ) / 2; } protected double w() { return ( FLOORED * Math.pow( base, FLOORED + 1 ) - ( FLOORED + 1 ) * Math.pow( base, FLOORED ) + 1 ) / (base - 1) + ( n - Math.pow( base, FLOORED ) ) * CEILINGED; } public double getResult() { return ( this.f() - this.g() - this.h() + n ) / ( base - 1 ) + ( this.v() - this.u() + this.w() + Math.ceil( Math.log( n ) / Math.log( base ) ) ); } public static void main(String[] args) { int n = 100; int base = 3; try { System.out.println("For n = " + n + ", and base = " + base); System.out.println(); System.out.println("\tComputation with functions:\t\t" + (int)new Equation(n, base).getResult()); } catch (Exception e) { e.printStackTrace(); } int sum = 0; for(int i = 1 ; i <= n; i ++) { for(int j = 1; j <= i; j ++) { for(int k = 1; k <= j; k *= base) { sum ++; } } } System.out.println("\tComputation with loops:\t\t\t" + sum); } }
Owner

### mohamed-ennahdi commented Jun 29, 2014

 Tested on JavaSE-1.7.