Do-While iteration approach determine by threshold - java

I have a question using Java do-while loop to determine when to stop by a threshold (MinRevChange in the code).
The larger loop will stop when the value difference between 2 closest data points is below the threshold (DfDi < MinRevChange); and the loop within it will stop when Q is greater than n (30 in this example).
private static int OBMethod33(double mean, double MinRevChange) {
double RmRev = 399;
double BenchPrice = 486;
int n = 30;
int i = 0;
double Fcurr = 0;
double Fopt = 0;
double Fprev = 0;
double DfDi = 999999;
do {
Fcurr = 0;
int Q = 0;
do {
Fcurr = Fcurr
+ RmRev * (Math.pow(mean, Q) * Math.exp(-mean) / factorialRecursive(Q)) * Math.max(Q - i, 0)
+ BenchPrice * (Math.pow(mean, Q) * Math.exp(-mean) / factorialRecursive(Q))
* Math.max(i - Q, 0);
Q++;
} while (Q <= n);
DfDi = Math.abs(Fcurr - Fprev);
Fopt = Fcurr;
Fprev = Fcurr;
i++;
} while ((i <= 10) && (DfDi >= MinRevChange));
return i - 1;
}
private static long factorialRecursive(int i) {
if (i < 0) {
return -1;
}
if (i == 0) {
return 1;
}
if (i < 2)
return i * 1;
return i * factorialRecursive(i - 1);
}
Based on Poisson Distribution pmf function: Poisson Distribution
which is the code (Math.pow(mean, Q) * Math.exp(-mean) / factorialRecursive(Q))
As Q increases to n, 'Fcurr' will be added for each Q.
DfDi is calculated by deducting the value of two 'i's: 'Fcurr' and 'Fprev'.
The second function returns factorial of a number i! = 1 * 2 * ... * i; used to calculate Poisson Distribution pmf.
Given an example of (mean, MinRevChange) = (6.2, 250) it returns 10;
while I validated the example and function using SAS, it should return 5.
I think within the first function, it seems the 'DfDi >= MinRevChange' condition is not used; How can I modify it? Thanks!

The following code works:
private static int OBMethod33(double mean, double MinRevChange) {
double RmRev = 399;
double BenchPrice = 486;
int n = 30;
int i = 0;
double Fcurr = 0;
double Fopt = 0;
double Fprev = 0;
double DfDi = 999999;
do {
Fcurr = 0;
int Q = 0;
do {
Fcurr = Fcurr
+ RmRev * (Math.pow(mean, Q) * Math.exp(-mean) / factorialRecursive(Q)) * Math.max(Q - i, 0)
+ BenchPrice * (Math.pow(mean, Q) * Math.exp(-mean) / factorialRecursive(Q))
* Math.max(i - Q, 0);
Q++;
} while (Q <= n);
DfDi = Math.abs(Fcurr - Fprev);
Fopt = Fcurr;
Fprev = Fcurr;
i++;
// This line added to print out the result after each iteration;
System.out.println("DfDi = " + DfDi + " :: Fcurr = " + Fcurr);
} while ((i <= 10) && (DfDi >= MinRevChange));
return i - 1;
}
private static long factorialRecursive(int i) {
if (i < 0) {
return -1;
}
if (i == 0) {
return 1;
}
if (i < 2)
return i * 1;
return i * factorialRecursive(i - 1);
It works by setting value pairs of (mean, MinRevChange).
Thanks!

Related

Finding the square root of a number by using binary search

I tried using binary search to find the square root of an integer, but some how I couldn't pass a few test cases.
I was able to pass mySqrt(4) = 2, but I wasn't able to pass mySqrt(2147395599)
Any ideas on where I messed up?
public static int mySqrt(int x) {
int left = 0;
int right = x;
if(x < 2){
return x;
}
while(left < right){
int mid = left + ((right - left) / 2);
if(mid * mid == x){
return mid;
}
else if(mid * mid < x){
left = mid + 1;
}
else{
right = mid;
}
}
return left - 1;
}
Because mid * mid will overflow. You should use long to avoid the overflow. Then cast it back to int when you return the result.
Try this code
public static int mySqrt(int x) {
long left = 0;
long right = x;
if(x < 2){
return x;
}
while(left < right){
long mid = left + ((right - left) / 2);
if(mid * mid == x){
return (int)mid;
}
else if(mid * mid < x){
left = mid + 1;
}
else{
right = mid;
}
}
return (int)(left - 1);
}
Here is a version which handles doubles.
It is recursive.
It computes until a given precision is reached.
for (int i = 2; i < 10; i++) {
System.out.println("sqrt("+i+") = " + sqrt(i));
}
Prints
sqrt(2) = 1.414213562373095
sqrt(3) = 1.7320508075688772
sqrt(4) = 2.0
sqrt(5) = 2.23606797749979
sqrt(6) = 2.449489742783178
sqrt(7) = 2.6457513110645907
sqrt(8) = 2.82842712474619
sqrt(9) = 3.0
public static double sqrt(double i) {
return bsqrt(i, 0, i, 0);
}
static double prec = 10E-200;
private static double abs(double d) {
return d < 0 ? -d : d;
}
private static double bsqrt(double i, double low, double high,
double last) {
double mid = (high + low) / 2;
double d = last - mid;
if (d < 0) {
d = -d;
}
if (d < prec) {
return mid;
}
double sqr = mid * mid;
if (sqr < i) {
return bsqrt(i, mid, high, mid);
} else {
return bsqrt(i, low, mid, mid);
}
}
But a better way is to use Newton's method.
static double prec = 10E-15;
public static double newtons(double i) {
// initial guess
double x = i / 2;
double d = i;
double nx = 0;
while (abs(d) > prec) {
nx = x - (x*x - i)/(2*x);
d = nx - x;
x = nx;
}
return nx;
}
This is like a binary search
The following code computes the [integer] square root of a number. If the number is not a
perfect square (there is no integer square root) , then it returns -1. It does this by successive
guessing. If n is 100, it first guesses SO. Too high? Try something lower - halfway between 1
and SO. What is its runtime?
int sqrt(int n) {
return sqrt_helper(n, 1, n);
}
int sqrt_helper(int n, int min, int max) {
if (max < min) return -1; // no square root
int guess = (min + max) / 2·,
if (guess *guess == n) { // found it!
return guess;
}else if (guess * guess < n) { II too low
return sqrt_helper(n, guess + 1, max); // try higher
} else { // too high
return sqrt_helper(n, min, guess - l); //try lower
}
}
credits: cracking the coding interview

Simpson's double integral won't work in java

So I had to write an algorithm for the Simpson's double integral so I can find the answer in a much faster manner. I had a guide that showed the steps to follow to write this program. After following this guide and running it in netbeans, I found out that the values coming out of the program where not really close to the real answer. This is the code that I written in java:
//INPUT
double a = 0,b = 1; // Endpoints
int m = 8,n = 4;
double K1 = 0, K2 = 0, K3 = 0;
//OUPUT
//Step 1
double h = (b - a) / n;
double j1 = 0; //End terms
double j2 = 0; //Even terms
double j3 = 0; //Odd terms
//Step 2
for(int i = 0; i <= n; i++){
//Step 3
double x = a + (i * h);
double hX = (d(x) - c(x)) / m;
K1 = f(x, c(x)) + f(x, d(x)); // End terms
//Step 4
for (int j = 1; j < (m-1); j++){
//Step 5
double y = c(x) + (j * hX);
double q = f(x,y);
//Step 6
if (j % 2 == 0){
K2 = K2 + q;
}
else
K3 = K3 + q;
}
//Step 7
double l = (K1 + (2*K2) + (4*K3)) * (hX / 3);
//Step 8
if (i == 0 || i == n)
j1 = j1 + l;
else if (i % 2 == 0)
j2 = j2 + l;
else
j3 = j3 + l;
}
double j = h * (j1 + (2 * j2) + (4 * j3)) / 3;
System.out.println("j = " + j);
}
public static double c(double x){
return x;
}
public static double d(double x){
return 2 * x;
}
public static double f(double x, double y){
return (Math.pow(y, 2) + Math.pow(x, 3));
}
I tried debugging the program several times but I haven't yet found why I am encountering this mistake. If there's any mistake that you find in my code please let me know to see if it fixes it. For the given example, I am getting the value of 0.9069281684027777 instead of having the correct value which is 0.7838542. Thank you for your help. You can also see the guide that I followed to be able to create this program.
I did not check the math, the large error seems to indicate an error in the algorithm implemented. The for-bounds are dubious. And floating point errors exist.
Instead of multiplying a fraction by a running index (which would multiply the floating point approximation error in the fraction), better do:
Instead:
double h = (b - a) / n;
for (int i = 0; i <= n; i++) {
double x = a + (i * h);
do
for (int i = 0; i < n; i++) {
double x = a + i * (b - a) / n;
or
for (int i = 0; i <= n; i++) {
double x = a + i * (b - a) / (n + 1);
The boundary n being a bit unclear to me.

Program to generate and print all the "anagrams" of a number without using array

This is a Java program to generate and print all the possible "Anagrams" of a four digit number without using an array. Here is what I've been able to do so far:
import java.util.*;
class Anag {
public static void main() {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int d = n % 10;
n = n / 10;
int c = n % 10;
n = n / 10;
int b = n % 10;
n = n / 10;
int a = n;
int w = a, x = b, y = c, z = d, co, i, j;
System.out.println(w * 1000 + x * 100 + y * 10 + z * 1);
for (i = 1; i <= 4; i++) {
for (j = 1; j <= 3; j++) {
if (j % 3 == 1) {
co = w;
w = x;
x = co;
System.out.println(w * 1000 + x * 100 + y * 10 + z * 1);
}
if (j % 3 == 2) {
co = x;
x = y;
y = co;
System.out.println(w * 1000 + x * 100 + y * 10 + z * 1);
}
if (j % 3 == 0) {
co = y;
y = z;
z = co;
System.out.println(w * 1000 + x * 100 + y * 10 + z * 1);
}
}
}
}
}
Using the above code, I've been able to generate 12 "Anagrams", but I cannot figure out how to generate the remaining 12 (there should be 24 in total). Does anyone have any ideas?
The following algorithm should work for you. In short, you shiftrotate the number and for every 4 anagrams you swap first two digits, but after the 12th anagram you swap 1st and 3rd digit.
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int d = n % 10;
n = n / 10;
int c = n % 10;
n = n / 10;
int b = n % 10;
n = n / 10;
int a = n;
int t;
for (int i = 0; i < 24; i++) {
System.out.println(a * 1000 + b * 100 + c * 10 + d);
if (i == 11) {
t = a;
a = c;
c = t;
}
if (i % 4 == 3) {
t = a;
a = b;
b = t;
} else {
t = a;
a = b;
b = c;
c = d;
d = t;
}
}
}

nested for() loops && using the "i" count from the first loop in the second so i loops just once using the value?

i am trying to not import the math class to use but i am still trying to estimate the constant "e". it is said e= 1+(1/1!)+(1/2!)+(1/3!)+(1/4!)+(1/5!)+.....
these are what i have int at the top
String userInput;
int uIp; // this converts the string into int type
double e = 2;
then i ask some questions then i check to see not zero to exit and non negative to continue
While(uIp >0){
final int endTheLoop = 15;
int factorialNumber = 1;
double e2TheUserInput=0;
for(int i = 2; i < endTheLoop; i++){
for(int j = 1; j < i; j++){
factorialNumber = ((i - 1) * factorialNumber);
}
e = (1/factorialNumber) + e;
e2TheUserInput = Math.pow(e,uIp);
}
}
You are doing integer division(but e is a double right?):
e = (1/factorialNumber) + e;
Correct that to:
e = (1.0/(double)factorialNumber) + e;
It was counting all the loops, but changes are zero according to the integer division. :)
e= 2+(0)+(0)+(0)+(0)+.....
I am not sure what your code is trying to do but if you want to compute exp(x) this is how I would do it.
public static void main(String... args) {
for (int i = -4; i <= 4; i++)
System.out.println(i + ": " + exp(i) + " cf " + Math.exp(i));
}
private static double exp(double d) {
if (d < 0)
return 1 / exp(-d);
double e = 1, term = 1;
for (int i = 1; i < 20 || term > e * 1e-16; i++) {
term *= d / i;
e += term;
}
return e;
}
For large exponents, it more efficient to evaluate the integral powers without using a taylor series.
public static final double E = 2.7182818284590452354;
private static double exp(double d) {
if (d < 0)
return 1 / exp(-d);
long num = (long) d;
double numE = 1;
double mult = E;
while (num > 0) {
if ((num & 1) != 0)
numE *= mult;
num >>>= 1;
mult *= mult;
}
double fract = d - (long) d;
double fractE = 1, term = 1;
for (int i = 1; i < 20; i++) {
term *= fract / i;
fractE += term;
}
return numE * fractE;
}

Detecting fundamental frequency with comb filter

I need to use comb filter/transform to detect fundamental frequency of a wav file in java.
I also needed to implement ZCR, but this was easy.
Now I have this:
int best = 0, best_step = 0;
for (int step = 3; step < 400; ++step) {
int sum = 0;
for (i = 1; i < 10 && i * step < spectrum.length; ++i) {
for (int di = 0; di < i; ++di) {
sum += spectrum[i * step + di] / i;
}
}
sum *= 100;
comb.add(sum);
}
int sum = 0;
for (i = 0; i < comb.size(); ++i) {
sum = comb.get(i); // 3 * comb[i] - comb[i-1] - comb[i+1];
System.out.println(i + " - " + sum);
if (sum > best) {
best_step = i;
best = sum;
}
}
And my problem is that this code detects the wrong frequency. ;( I have searched for an algorithm/implementation (in any language) but have not found anything.
Note, I cannot use autocorelation, etc.. It must be comb filtering.
Edit: A little more explanation of my code:
I load a wav file and put frames to array frames. Then I make fft on it and have array of Complex (named widmo) (simple structure to handle complex numbers).
Now I put abs of Complex numbers into array spectrum:
double[] spectrum = new double[widmo.length];
for (i = 0; i + 1 < widmo.length; ++i) {
spectrum[i] = widmo[i].abs();
}
ArrayList<Integer> comb = new ArrayList<Integer>();
int best = 0, best_step = 0;
for (int step = 3; step < 400; ++step) {
int sum = 0;
for (i = 1; i < 10 && i * step < spectrum.length; ++i) {
for (int di = 0; di < i; ++di) {
sum += spectrum[i * step + di] / i;
}
}
// sum /= step + 100; // ta linijka pozwala usunąć sporo
// niespodziewanych skoków częstotliwości
sum *= 100;
comb.add(sum);
}
int sum = 0;
for (i = 0; i < comb.size(); ++i) {
sum = comb.get(i); // 3 * comb[i] - comb[i-1] - comb[i+1];
// ctx.fillRect(i, canvas.height, 1, -sum);
System.out.println(i + " - " + sum);
// tmp.add(new freqTime(sum,));
if (sum > best) {
best_step = i;
best = sum;
}
}
System.out.println();
System.out.println(best_step);
System.out.println(4 * 44100);
System.out.println((frames.length / numChanels));
System.out.println(best_step * 44100
/ (frames.length / numChanels));
The last println should show me my fundamental frequency, but it doesn't,.
I got this in javascript from my friend.
I figure it out (i think:P). Last println gives basic freq :). Maybe someone will need it or even imporve it :)
ArrayList<double[]> okna = new ArrayList<>();
ArrayList<freqTime> lista = new ArrayList<freqTime>();
int po2 = (int) Math.pow(2,
Integer.parseInt((String) potega2Input.getText()));
po2 /= 2;
double[] triangles = new double[po2];
double maxWykres = 0;
int licznik = 0;
int licznik2 = 0;
int T = frames.length;
boolean wykresFlaga = false;
for (int k = 0; k < T; k += po2) {
licznik = 0;
licznik2 = 0;
double[] tmp = new double[po2];
Complex[] zespolone = new Complex[po2];
for (int i = k; i < k + po2; i++) {
if (i < T) {
tmp[licznik] = frames[i];
zespolone[licznik] = new Complex(frames[i], 0);
licznik2 = licznik;
} else {
tmp[licznik] = frames[licznik2];
zespolone[licznik] = zespolone[licznik2];
}
licznik++;
}
okna.add(tmp);
FFT fft = new FFT();
zespolone = fft.fft(zespolone);
double maxF = 0;
double maxFI = 0;
double maxH = findMaxComp(zespolone);
double[] doWykresu = new double[zespolone.length];
for (int a = 2; a < 100; a++) {
for (int i = 0; i < po2; i++) {
doWykresu[i] = zespolone[i].abs();
triangles[i] = Math.abs(i % (2 * a) - a)
* (maxH) / a;
// triangles[i] = Math.abs(i % (2 * a) - a) * (maxH)
// / a;
}
double sumT = 0;
for (int i = 0; i < po2 / 2; i++) {
sumT += triangles[i] * doWykresu[i];
}
if (sumT > maxF) {
maxF = sumT;
maxFI = a;
}
}
//
// maxFI /= 2;
//
if (wykresFlaga == false) {
maxWykres = maxH;
}
for (int i = 0; i < po2; i++) {
doWykresu[i] = zespolone[i].abs();
triangles[i] = Math.abs(i % (2 * maxFI) - maxFI)
* (maxWykres) / maxFI;
}
if (wykresFlaga == false) {
System.out.println("Max w widmie: " + maxWykres);
new Wykres(doWykresu, 1, triangles);
wykresFlaga = true;
}
// System.out.println((2 * 44100 / po2) * maxFI);
System.out.println((float) (44100 / (float) po2)
* 2*(float) maxFI + " Znalzione a: " + maxFI);
lista.add(new freqTime(
(int) ((float) (44100 / (float) po2) *2* (float) maxFI),
(double) ((double) po2 / 44100)));
/*
* System.out.println((44100 / po2) * maxFI + " " + maxFI +
* " " + maxFI / 44100 + " " + 44100 / (maxFI / po2 * 44100)
* + " " + 44100 * maxFI / T);
*/
// System.out.println(zespolone[(int) maxFI/2].abs());
}

Categories

Resources