How to calculate the sum of geometric series [duplicate] - java

This question already has answers here:
What does the ^ operator do in Java?
(19 answers)
Closed 3 years ago.
I want to calculate the sum of a geometric series. ie: 1 , 5 , 25 , 125 , etc
I try to use the math formula to calculate it: a(r^n -1)/(r-1)
My code:
int a = 1;
int r = 5;
int deno = r -1;
int n = 3
int rn = r^n -1 ;
int total = a * rn / deno;
Apparently there is wrong with the code and only some values like the example I give works. I do not know why QAQ
I think the problem is the symbol ^
can anyone explain what ^ does in java? Appreciate it

Atleast in Java 7, the symbol ^ does not mean power.
Try this, you would also like to put a condition where r>1 or r<1. Both have different formuals.
int a = 1;
int r = 5;
int deno = r -1;
int n = 3;
double sum=a*(Math.pow(r, n)-1)/deno;

Related

Java Math.sqrt function rounding down to zero [duplicate]

This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
How to divide 1 by 25(1/25) and get result .04 in java [duplicate]
(4 answers)
Closed 1 year ago.
I am trying to write a function to check a T score value and populate half of a 5x5 array.
public void calcTScores()
{
double temp = 0;
double tempSp = 0;
int n = 24;
this.tScores = new String [5][5];
for (int i = 0; i< this.Headers.length; i++){
for (int j = 0; j<this.Headers.length; j++)
{
if(i < j)
{
tempSp += (n-1)*this.SD[i] * this.SD[i] + (n-1)*this.SD[j] * this.SD[j];
tempSp = tempSp/(n+n-2);
tempSp = Math.sqrt(tempSp);
temp = tempSp * Math.sqrt(0.0833);
System.out.println(Math.sqrt(1/12));
temp = ((this.Mean[i] - this.Mean[j])/temp);
if(temp > 2.25 || temp< -2.25)
{
this.tScores[i][j] = "Y";
}
else
{
this.tScores[i][j] = "N";
}
temp = 0;
tempSp = 0;
}
}
}
}
Any idea why Math.sqrt(0.0833) and Math.sqrt(1/12) would evaluate to different values?
The T score when I add the 1/24 and 1/24 value and take the sqrt keeps evaluating to zero but when I plug in the actual decimal it gives me the answer I would expect
Any ideas why this is occuring?
1/12==0 as per integer division
There's nothing wrong with Math.sqrt. You're passing it the number zero.
Math.sqrt(1/12)
1/12 is a division operation on two integers (namely, 1 and 12), so it's going to produce an integer result. 12 goes into 1 zero times, so the integer result is zero. Thus, your expression is
Math.sqrt(0)
Consider
Math.sqrt(1.0/12.0)
Math.sqrt(1/12d)
Cast 1/12 to a double by casting one operand to a double.

Summing a number's digits in Java [duplicate]

This question already has answers here:
Separating the Digits in an Integer - exercise from Deitel's Java book
(11 answers)
Closed 7 years ago.
Let's say you have an integer '75'. Normally, in your head, you can add the '7' with the '5' in order to get '12'. So you split the number '75' into two different numbers 7 and 5 then add them together. That leads to my question, how can you perform that in java? Is there a Math method that does it for you?
You can use plain maths
int i = 75;
int a = i / 10; // 7
int b = i % 10; // 5
int c = a + b; // 12
You can use some code like:
int num=75;
int sum_digits=0;
while(num>0){
int digit = num%10;
num /= 10;
sum_digits += digit;
}

Java keeps returning incorrect answer [duplicate]

This question already has answers here:
What does the ^ operator do in Java?
(19 answers)
Closed 8 years ago.
a is the value of the spinner.
private void toolCalculateActionPerformed(java.awt.event.ActionEvent evt) {
Integer a = (int) toolSpinner.getValue();
if (toolEnch.getSelectedIndex() == 0) {
double p;
p = (10 ^ (2 - a) * 13 ^ a);
double x = Math.round(p);
System.out.println(x);
}
}
I am doing 10^(2-a) * 13^a, and
lets say the spinner is at 1, it returns 6, when it should return 130.
lets say the spinner is at 2, it returns 8, when it should return 169.
I've tested it with WolframAlpha and it gives me the right result. This program however, gives me something way off.
Any ideas on how to fix this?
^ is XOR, not exponent. If you want exponents, use Math.pow.
p = Math.pow(10, (2-a)) * Math.pow(13, a);
your operations is write in wrong priority,and ^ is XOR not POW,actually you must handle priority's by your self using parenthesis.because parenthesis have high priority more than each operation.
use Math Class
change p variable line like this:
p = Math.pow(10, (2-a)) * Math.pow(13, a); // it will be 130.0 in double format when a is 1
and if you want calculate anything else like XOR
you must handle periority's by your self in most time's!
like this:
int a = 1;
double p = 0;
p = ((10 + (2-a)) * (13 + a));
System.out.println(p);; // this will be 154.0 in double format.
but This:
int a = 1;
double p = 0;
p = (10 + (2-a) * (13 + a));
System.out.println(p); // will print 24.0 in double format.

How to create a random number with range? [duplicate]

This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 9 years ago.
I am trying to create a random phone number with a range. The format being (xxx)-xxx-xxx and the area code not starting with 0,8, or 9 and the next set of three being in a range from 100-742 and then the last set of 4 can be any digit. How would i create the first two parts? Any help would be appreciated. Thanks!
import java.util.*;
import java.text.*;
public class PhoneNumber{
public static void main(String[] arg){
Random ranNum = new Random();
//int areaCode = 0;
//int secSet = 0;
//int lastSet = 0;
DecimalFormat areaCode = new DecimalFormat("(000)");
DecimalFormat secSet = new DecimalFormat("-000");
DecimalFormat lastSet = new DecimalFormat("-0000");
//DecimalFormat phoneNumber = new DecimalFormat("(###)-###-####");
int i = 0;
//areaCode = (ranNum.nextInt()); //cant start with 0,8,9
//secSet = (ranNum.nextInt()); // not greater than 742 and less than 100
//lastSet = (ranNum.nextInt(999)) + 1; // can be any digits
i = ranNum.nextInt();
System.out.print(areaCode.format(i));
i = ranNum.nextInt();
System.out.print(secSet.format(i));
i = ranNum.nextInt();
System.out.print(lastSet.format(i));
}
}
well, basically, you need to generate numbers in two ranges
[1; 7]
[100; 742]
To have random integer in range [m; n] you could write:
updated (remove Math.random())
int numberInRange = m + new Random().nextInt(n - m + 1);
HTH
1,2,3,4,5,6,7
makes 7 different values
ranNum.nextInt(7)+1; //So 1 is your lowest number and 7 is the number of different solutions
nexInt will range between 0 and intPassed exclusive,
So ranNum.nextInt(7) will run between 0 and 6, + 1 makes 1 .. 7
This will range between 1 and 7
You can take the same principal for the second range
You can try the next:
int sec = java.util.concurrent.ThreadLocalRandom.current().nextInt(100, 743);
And so on with the other parts of the phone number.
This method returns a pseudorandom, uniformly distributed value between the given least value (inclusive) and bound (exclusive).
Just make a random number greater than 99 and less than 800, then the next would be about the same way.
Random rand = new Random();
// add 1 to make it inclusive
max min
int firstRandomSet = rand.nextInt((799 - 100) + 1) + 100;
//none starts with 0,8, or 9
int secondRandomSet = rand.nextInt((742 - 100) + 1) + 100;
//produces anything from 100-742
to get the numbers 0001 - 9999 you'll have to be creative.
int maxValues= 9999;
int thirdRandomSet = rand.nextInt(maxValues);
System.out.printf("%04d\n", thirdRandomSet);

how to generate positive and negative numbers in Java [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to generate random positive and negative numbers in java
Hello I am trying to create a method in Java to create negative and positive values in Java.
the problem is that I don't know how to get this programmed but I do know the logic.. here is what I tought it should be
Random generator = new Random();
for (int i = 0; i < 21; i++)
{
System.out.print(generator.nextInt(100) + 1);
System.out.println();
}
but with the code above I get only positive values and I need values between -100 and 100 but how can I accomplish something like that?
You can use:
Random generator = new Random();
int val = 100 - generator.nextInt(201);
Or, as JoachimSauer suggested in the comments:
int val = generator.nextInt(201) - 100;
The general formula is
int val = rand.nextInt(max - min + 1) + min;
Note that min and max can be negative. (max > min)

Categories