I have the following code
public static void main(String[] args) {
BigInteger iter = BigInteger.valueOf(140);
BigInteger y = BigInteger.valueOf(1114112);
BigInteger sum = BigInteger.valueOf(0);
while(iter.intValue() != 0) {
BigInteger z = BigInteger.valueOf((y.pow(iter.intValue())).longValue());
sum = sum.add(z);
iter = iter.subtract(BigInteger.valueOf(1));
System.out.println("Itereration: " + (140 - iter.longValue()));
System.out.println("Y: " + y.longValue());
System.out.println("Z: " + z.longValue());
System.out.println("Sum: " + sum.longValue());
}
}
However, the output is this (only last 3 iterations)
Iteration: 137
Y: 1114112
Z: 0
Sum: 0
Iteration: 138
Y: 1114112
Z: 1382886560579452928
Sum: 1382886560579452928
Iteration: 139
Y: 1114112
Z: 1241245548544
Sum: 1382887801825001472
Iteration: 140
Y: 1114112
Z: 1114112
Sum: 1382887801826115584
The rest of iterations 1-136 are the same as iteration 137
The .longValue() calls do completely wrong things on BigInteger values this large. If you tried using .longValueExact() instead, you'd see that it throws an exception because the values are out of range for a long. But if you don't do the unnecessary .longValue() calls , the code works:
BigInteger iter = BigInteger.valueOf(140);
BigInteger y = BigInteger.valueOf(1114112);
BigInteger sum = BigInteger.valueOf(0);
while(iter.intValue() != 0) {
BigInteger z = y.pow(iter.intValue();
sum = sum.add(z);
iter = iter.subtract(BigInteger.valueOf(1));
System.out.println("Itereration: " + (140 - iter.longValue()));
System.out.println("Y: " + y);
System.out.println("Z: " + z);
System.out.println("Sum: " + sum);
}
And as #RC. has suggested in the comment, you could make iter a simple int, which simplifies the code further:
int iter = 140;
BigInteger y = BigInteger.valueOf(1114112);
BigInteger sum = BigInteger.valueOf(0);
while(iter != 0) {
BigInteger z = y.pow(iter);
sum = sum.add(z);
iter--;
System.out.println("Itereration: " + (140 - iter));
System.out.println("Y: " + y);
System.out.println("Z: " + z);
System.out.println("Sum: " + sum);
}
Related
Here I got a function like this and I want to find the recursive relation of that and after that calculate time complexity of that recursive relation.
public static void f(int n) {
if (n == 1) {
//" Do sth "
} else {
for (int i = 1; i < n; i++) {
f(i - 1);
//" Do sth "
}
}
}
actually I tried a lot for that and I got T(n) = n * f( n-1) for this function as the relation but I am not sure about that . could you help me find the correct relation and solve it ?
Assuming T(1) = "Do sth" is constant work i.e. it doesn't depend on the input size n, you could write the recursive time function as :
T(n) = T(1) + T(2) + ... + T(n-1)
= { T(1) } + { T(1) } + { T(1) + T(2) } + { T(1) + T(2) + T(3) } + { T(1) + T(2) + T(3) + T(4) } +....
[let T(1) = x]
= x + x + {x + x} + {x + x + (x + x)} + {x + x + (x + x) + x + x + (x + x)} +....
= x + x + 2x + 4x + 8x + ...
~ x.2^(n-2)
~ O(2^n)
Here is a python program to demonstrate the sequence of coefficients for the summation:
t = [0 for i in range(10)]
for i in range(1,10):
if i == 1:
t[i] = 1
else:
val = 0
for j in range(1,i):
val += t[j]
t[i] = val
print(t[1:])
prints : [1, 1, 2, 4, 8, 16, 32, 64, 128]
You can see that 2(n-2) for n >= 2 holds good at each 'n' and complexity is O(2n)
I'm a complete beginner in Java who just recently got introduced to loops. I'm trying to do write a program that reads in a target and finds the first n such that 1 + 1/2 + 1/3 + ... + 1/n > target. The problem supplied a code with the initialization of n and sum missing as well as the condition of while and its statements missing.
I'm able to work out how to make the harmonic series loop, but I'm not sure what to set n with to stop the loop when it exceeds the target.We've not learned about arrays in class yet..
import java.util.Scanner;
/**
This program computes how many steps the sum 1 + 1/2 + 1/3 + ...
needs to exceed a given target.
*/
public class ReciprocalSum
{
public static void main(String[] args)
{
double sum = 0;
int n = ???? ;
Scanner in = new Scanner(System.in);
System.out.print("Target: ");
double target = in.nextDouble();
int i = 0;
//Notes
// 1 + 1/2 + 1/3 + 1/4 + ..... 1/n
//Make a loop that repeats itself starting with n = 1 --> 1/1 + 1/2 + 1/3 + 1/4 + 1/n
// 1.0/n + (1.0/ n - 1) + (1.0/n-2) +.... if n =4 --> 1/4 + 1/3 + 1/2 + 1/1 as long as n >0
while ( n > 0)
{
sum += 1.0/n ;
n--;
}
System.out.println("n: " + n);
System.out.println("sum: " + sum);
}
}
n should be incremented in the loop (and therefore it should start at 0), and the loop should be exited when you reach the target:
int n = 0;
...
while (sum <= target)
{
n++;
sum += 1.0/n;
}
Because Java 8+ has lambdas, and you can generate a range 1 to n and perform your calculation and get the sum in one step. Basically, you could do
Scanner in = new Scanner(System.in);
System.out.print("Target: ");
double target = in.nextDouble(), sum = 1.0;
int n = 1;
while (sum < target) {
sum = IntStream.range(1, n).mapToDouble(i -> 1.0 / i).sum();
n++;
}
System.out.printf("n=%d, sum=%.2f%n", n, sum);
You can achieve that in this way by calculating sum of the series until its sum bigger than the target value:
double sum = 0;
int n = 1;
Scanner in = new Scanner(System.in);
System.out.print("Target: ");
double target = in.nextDouble();
while(sum <= target){
sum = sum + 1.0/n;
n = n + 1;
}
System.out.println(sum);
I'm getting a weird output from running this simple java program.
The output is: 0 4 2 -6
Why does the x++ print 0, it should be printing 4.
import java.util.*;
import java.io.*;
public class Java1 {
public static void main(String[] args) throws IOException {
int x = 4;
int y = -5;
System.out.println(x++ + " " + func(x++, y) + " " + --y);
}
public static int func(int work, int y) {
int z = work + y;
work++;
y++;
System.out.print(z + " ");
return z + work + y;
}
}
Okay, here is what's going on: First x++ is evaluated, returning 4 (which is later printed) and leaving x at 5. Then x++ is evaluated again, passing 5 to func. Then func is evaluated with 5 and -5 parameters. In here z is 0 (5 + (-5) = 0) which is then printed (BEFORE the println in the main method. func then returns 2 (0 + 6 + (-4)) which is also added to the string. Finally --y results in -6. Now the println in the main method prints its string (4 2 -6).
func(x++, y) is executed first, so 0 comes from System.out.print(z + " "); in func.
System.out.print(z + " ");
is executed before
System.out.println(x++ + " " + func(x++, y) + " " + --y);
So the 0 comes from z, not x.
I have mentioned the flow of points from 0 to 7 in the comments
public static void main(String[] args) throws IOException {
int x = 4;
int y = -5;
System.out.println(x++ + " " + func(x++, y) + " " + --y);
// thus 0]4 6]2 (value returned as z) 7] localvalue of --y as -6
}
//1] x++ makes x as 5 when it is passed to func()
public static int func(int work, int y) {
int z = work + y;
//2] z = 5 + -5 = 0
work++;
//3] work which was x as 5 is now 6
y++;
//4] y will be -4 now
System.out.print(z + " ");
return z + work + y;
//5] z = 0 + 6 + -4 = 2 and is returned to func() caller
}
import java.util.*;
import java.io.IOException;
public class Java1
{
public static void main(String args[])
{
int x = 4;
int y = -5;
System.out.println("x = "+ (x++ ) +" func = "+ (func(x++, y) ) + " y = "+ --y);
}
public static int func(int work, int y)
{
int z = work + y;// 5+-5 = 0
work++; //6
y++; //-4
System.out.print("Z = " + z + " ");//0
return z + work + y; //0 + 6+-4 = 2
}
}
OUTPUT :
Z = 0 x = 4 func = 2 y = -6
Here the func() is executed first and hence the value of variable z is printed as 0 and then the x++ value is printed as 4.
I managed to solve this, below is my solution :
public class ProblemA001k {
public static void main(String[] args) {
System.out.println("Sum from 1" + " to " + divQ + ":" + sum2);
System.out.println();
divQ += q;
newQ += q;
sum1 = 0;
sum2 = 0;
}
key.close();
}
}
Now I was told to modify my solution so that it uses ONLY ONE LOOP.
I have 3 loops in the code above, even when I tried using only 2 loops I struggled. but ONE LOOP ? I don't know how to improve my code. Please help me.
This is a Mathematic problem.
If you know that you can find the sum of all integers from 1 to X, you just need to do X * (X+1) / 2.
You can find all the batch values easily.
Sum from 1 to 400: 80200
Sum from 401 to 450: 21275
Sum from 1 to 450: 101475
Will be found like this :
450 * 451 / 2 = 101475 (1 to 450)
400 * 401 / 2 = 80200 (1 to 400)
101475 - 80200 = 21275 (401 to 450)
With this, you can limit the loop to just calculate the values from q to n by incrementing by q
And a quick code to do it :
static void sum(int n, int q){
int i = q;
int sum, tmp=0;
while(i < n){
sum = i * (i+1) / 2;
System.out.println(String.format("Sum from %d to %d : %d", i-q+1 , i, sum - tmp));
System.out.println(String.format("Sum from %d to %d : %d", 1, i, sum));
tmp = sum;
i += q;
}
}
And I run it with
public static void main(String[] args){
sum(500, 50);
}
to have this result
Sum from 1 to 50 : 1275
Sum from 1 to 50 : 1275
Sum from 51 to 100 : 3775
Sum from 1 to 100 : 5050
Sum from 101 to 150 : 6275
Sum from 1 to 150 : 11325
Sum from 151 to 200 : 8775
Sum from 1 to 200 : 20100
Sum from 201 to 250 : 11275
Sum from 1 to 250 : 31375
Sum from 251 to 300 : 13775
Sum from 1 to 300 : 45150
Sum from 301 to 350 : 16275
Sum from 1 to 350 : 61425
Sum from 351 to 400 : 18775
Sum from 1 to 400 : 80200
Sum from 401 to 450 : 21275
Sum from 1 to 450 : 101475
The good think with this solution is the number of loop, this will increment by q instead of 1
Note : The solution is a quick implementation, this could be done better.
EDIT :
Thanks to Margaret Bloom in the comments to point out the name of this formula :) For more information, you are welcome to look at Triangular Number
This should do it:
int totalSum = 0;
int batchSum = 0;
for (int i = 1; i <= n; i++) {
totalSum += i;
batchSum += i;
if (i % q == 0) {
System.out.println("Sum from " + (i - q + 1) + " to " + i + ":" + batchSum);
System.out.println("Sum from 1 to " + i + ":" + totalSum);
batchSum = 0;
}
}
Edit:
The better Math way:
int lastTotalSum = 0;
for (int i = 1; i <= n / q; i++ ) {
int top = i * q;
int totalSum = top * (top + 1) / 2;
int batchSum = totalSum - lastTotalSum;
System.out.println("Sum from " + (top - q + 1) + " to " + top + ":" + batchSum);
System.out.println("Sum from 1 to " + top + ":" + totalSum);
lastTotalSum = totalSum;
}
I found a nice solution with java8 Streams:
int n=1000;
int q=50;
int length = n/q -1;
int[] previousSum={0};
IntStream.range(0, length).map(i -> (i+1)*q).forEach(s -> {
int sum=(s*(s+1))/2;
int batch = sum - previousSum[0];
previousSum[0] = sum;
System.out.println("Sum from " + (s - q + 1) + " to " + s + ":" + batch);
System.out.println("Sum from 1 to " + s + ":" + sum);
});
Do one loop iterating entire range and use indexes to decide whether to add, reset or print your sums.
Hope this gives your right idea, if you still dont know I can illustrate it a bit more.
I'm currently learning Java and I've just beginned so my knowledge of it is not very good.
I have a problem with a program I wrote that calculates the first 100 values of the Fibonacci sequence. The point is that it just outputs the 2 and no other number.
This is the code of my program:
class MyClass1 {
public static void main(String[ ] args) {
int[] fib = new int[102];
fib[0] = 1;
fib[1] = 1;
int counter = 0;
int n1, n2, fibSum;
while(counter < (fib.length - 2)){
n1 = fib[counter];
System.out.println(fib[counter]);
counter++;
n2 = fib[counter];
System.out.println(n2);
counter++;
fibSum = n1 + n2;
System.out.println(fibSum);
fib[counter] = fibSum;
}
}
}
Thank you for your help.
There are some logical errors in your code.
First loop:-
Initially n1=fib[0]=1 and n2=fib[1]=1 and you print both. fib[2] is the sum and so it is 2. So far so good.
Second loop:-
n1 = fib[2] = 2. n2 = fib[3] = 0 and hence fib[4] = 2. This is where the problem happens. Hence you will always see 2 0 2 in the output from second loop onwards.
For Fibonacci sequence, you need to add the previous 2 values but you are only considering the previous value in your code. Here's a corrected version of your code:-
public static void main(String[ ] args) {
double[] fib = new double[100];
fib[0] = 1;
fib[1] = 1;
int counter = 2;
double n1, n2, fibSum;
System.out.println(fib[0]);
System.out.println(fib[1]);
while(counter < fib.length){
n1 = fib[counter-1];
n2 = fib[counter-2];
fibSum = n1 + n2;
System.out.println(fibSum);
fib[counter] = fibSum;
counter++;
}
}
Note that I am using type double because type int or even long is not enough for going upto the 100th term in this sequence.
Fibonacci number is the sum of the previous 2 numbers:
fibonacci(n) = fibonacci(n - 1) + fibonacci(n - 2)
So it can be evaluated very nice using recursion:
private static long fibonacci(int n) {
if (n <= 1) return n;
else return fibonacci(n - 1) + fibonacci(n - 2); }
public static void main(String[] args) {
int n = 102;
for (int i = 1; i <= n; i++)
System.out.println(i + ": " + fibonacci(i));
}
Your problem is that
A) all array values are initialized to 0
B) you are accessing "the next" array value, before assigning a value to it!
When you change your outputs to:
System.out.println("1. counter: " + counter + "/" + fib[counter]);
counter++;
n2 = fib[counter];
System.out.println("2. counter: " + counter + "/" + fib[counter]);
counter++;
fibSum = n1 + n2;
fib[counter] = fibSum;
System.out.println("3. counter: " + counter + "/" + fib[counter]);
You will find that it prints:
1 counter: 0/1
2 counter: 1/1
3 counter: 2/2
1 counter: 2/2
2 counter: 3/0
The last row shows you what is going on: you fetch the value at index 3 ... before you assigned a value to it. Therefore your whole fibonacci sum ... doesn't "start"; because you keep loosing values.
In other words: you have to ensure that your counter increases properly:
while(counter < (fib.length - 2)){
n1 = fib[counter];
System.out.println("1 counter: " + counter + "/" + fib[counter]);
n2 = fib[counter+1];
System.out.println("2 counter: " + counter + "/" + fib[counter+1]);
fibSum = n1 + n2;
fib[counter+2] = fibSum;
System.out.println("3 counter: " + counter + "/" + fib[counter+2]);
counter++;
}
Meaning: you need one full loop for each value of counter. When you run my solution, you will find that it works nicely (until counter hits 44/45 and we run into int overflow; as the numbers get too big).
After your first iteration you'll have the following values:
n1 = 1
n2 = 1
fibsum = 2
fib[2] = 2
All other values from fib[3] to length are uninitialized.
So in the second iteration :
n1 = 2
now the counter value will be increased by 1 which is counter = 3
n2 = fib[3]
// this one in not calculated properly and hence the issue.