We had to create a Fibonacci system. Could you tell me what I did wrong? It gives me an error under the while loop but I'm sure it is the way I constructed my variables.
public class Chapter3 {
public static void main (String args[]){
int numFn;//CREATE NUMBER OF FN, SUM OF FN, AND AVERAGE
int average[]=new int [0];
int sumFn []=new int [0];//ARRAY OF SUMFN
numFn = 1;//ASSIGN FN AS 1
int x = 0;//NUMBERIN SIDE FN ARRAY
int Fn []=new int[16];//CREATE FN ARRAY
Fn [0]=0;
while (numFn <15){
Fn[x]= Fn[x]-Fn[x-1];//SET THE CURRENT FN NUMBER
sumFn [x]=sumFn[x]+(sumFn[x-1]+Fn[x]);//SET CURRENT SUMFN NUMBER
average [x]= sumFn[x]/numFn;
System.out.println(numFn +"/t" +Fn+"/t" +sumFn+"/t" +average);
x++;
numFn++;
}
}
}
well i changed it up using youre guys's advice yet the first ouput is 1 then 0 for everything, used this code :
public class Chapter3 {
public static void main (String args[]){
int numFn;//CREATE NUMBER OF FN, SUM OF FN, AND AVERAGE
int average[]=new int [16];
int sumFn []=new int [16];//ARRAY OF SUMFN
numFn = 1;//ASSIGN FN AS 1
int x = 1;//NUMBERIN SIDE FN ARRAY
int Fn []=new int[16];//CREATE FN ARRAY
Fn [0]=0;
while (numFn <15){
Fn[x]= Fn[x]-Fn[x-1];//SET THE CURRENT FN NUMBER
sumFn [x]=sumFn[x]+(sumFn[x-1]+Fn[x]);//SET CURRENT SUMFN NUMBER
average [x]= sumFn[x]/numFn;
System.out.println(numFn +"\t" +Fn[x]+"\t" +sumFn[x]+"\t" +average[x]);
x++;
numFn++;
}
}
}
Several problems :
new int[0] means an empty array, which is not what you
want.
X value is 0 on the first loop execution, so Fn[X-1] is Fn[-1], which
would cause an ArrayOutOfBoundException.
Can you also be more explicit about the error you encounter please?
I think this is what you're after (this code starts with 1 and 1 and prints the first 20 terms)...
public class Fibonacci {
public static void main(String[] args) {
int n0 = 1, n1 = 1, n2;
System.out.print(n0 + " " + n1 + " ");
for (int i = 0; i < 18; i++) { // Loop for the next 18 terms
n2 = n1 + n0; //The next term is the sum of the previous two terms
System.out.print(n2 + " ");
n0 = n1; // The first previous number becomes the second previous number...
n1 = n2; // ...and the current number becomes the previous number
}
System.out.println();
}
}
As for your errors, read the other answers. Their advice is good. :)
Your sumFn array is declared with a length of 0. So any time you try to add any element to it, you will get an ArrayOutOfBoundException.
There are several problems with the code, even after fixing the one that causes the ArrayIndexOutOfBoundsException I doubt it will work. For starters, you must initialize the arrays you're using in the correct size:
int average[]=new int [16];
int sumFn []=new int [16];
The "x" variable should start in 1:
int x = 1;
Also, it's not clear what do you want to print, anyway the println() statement should be fixed
System.out.println(numFn +"\t" +Fn[x]+"\t" +sumFn[x] + "\t" +average[x]);
This was really helpful resolving the Fibonacci sequence exercise. However doesn't print the zero, so I added here...
/*
* FibonacciSequence.java
* ----------------------
* This program displays the values in the Fibonacci sequnece from F0 to F15.
*/
import acm.program.*;
public class FibonacciSequence extends ConsoleProgram{
public void run() {
int n0 = 1;
int n1 = 1;
int n2;
println("0" + "\n" + n0 + "\n" + n1);
for (int i = 0; i < 13; i++) { // Loop for the next 18 terms
n2 = n1 + n0; //The next term is the sum of the previous two terms
println(n2 + " ");
n0 = n1; // The first previous number becomes the second previous number...
n1 = n2; // ...and the current number becomes the previous number
}
println();
}
}
Related
Fibonacci sequence is defined as a sequence of integers starting with 1 and 1, where each subsequent value is the sum of the preceding two I.e.
f(0) = 1
f(1) = 1
f(n) = f(n-1) + f(n-2) where n>=2
My goal is to calculate the sum of the first 100 even-values Fibonacci numbers.
So far I've found this code which works perfectly to calculate the sum of even numbers to 4million , however I'm unable to find edit the code so that it stops at the sum of the 100th value, rather than reaching 4million.
public class Improvement {
public static int Fibonacci(int j) {
/**
*
* Recursive took a long time so continued with iterative
*
* Complexity is n squared.. try to improve to just n
*
*/
int tmp;
int a = 2;
int b = 1;
int total = 0;
do {
if(isEven(a)) total +=a;
tmp = a + b;
b = a;
a = tmp;
} while (a < j);
return total;
}
private static boolean isEven(int a) {
return (a & 1) == 0;
}
public static void main(String[] args) {
// Notice there is no more loop here
System.out.println(Fibonacci(4_000_000));
}
}
Just to show the console from #mr1554 code answer, the first 100 even values are shown and then the sum of all is 4850741640 as can be seen below:
Any help is appreciated, thanks!
You need to use BigInteger because long easily overflows as Fibonacci's scales quite easily. BigInteger is also tricky to check whether is an odd or even number, but you can use BigInteger::testBit returning boolean as explained in this answer.
Here is some complete code:
BigInteger fibonacciSum(int count, boolean isOdd) {
int i = 0;
BigInteger sum = BigInteger.ZERO;
BigInteger current = BigInteger.ONE;
BigInteger next = BigInteger.ONE;
BigInteger temp;
while (i < count) {
temp = current;
current = current.add(next);
next = temp;
if ((current.testBit(0) && isOdd) || ((!current.testBit(0) && !isOdd))) {
sum = sum.add(current);
i++;
}
}
return sum;
}
Or you can have some fun with Stream API:
BigInteger fibonacciSum(int count, boolean isOdd) {
final BigInteger[] firstSecond = new BigInteger[] {BigInteger.ONE, BigInteger.ONE};
return Stream.iterate(
firstSecond,
num -> new BigInteger[] { num[1], num[0].add(num[1]) })
.filter(pair ->
(pair[1].testBit(0) && isOdd) ||
(!pair[1].testBit(0) && !isOdd))
.limit(count)
.map(pair -> pair[1])
.reduce(BigInteger.ZERO, BigInteger::add);
}
In any way, don't forget to test it out:
#Test
void test() {
assertThat(
fibonacciSum(100, false),
is(new BigInteger("290905784918002003245752779317049533129517076702883498623284700")));
}
You said.
My goal is to calculate the sum of the first 100 even-values Fibonacci numbers.
That number gets very large very quickly. You need to:
use BigInteger
use the mod function to determine if even
For this I could have started from (1,1) but it's only one term so ...
BigInteger m = BigInteger.ZERO;
BigInteger n = BigInteger.ONE;
BigInteger sumOfEven= BigInteger.ZERO;
int count = 0;
BigInteger t;
while( count < 100) {
t = n.add(m);
// check if even
if (t.mod(BigInteger.TWO).equals(BigInteger.ZERO)) {
sumOfEven = sumOfEven.add(t);
count++;
}
n = m;
m = t;
}
System.out.println(sumOfEven);
Prints
290905784918002003245752779317049533129517076702883498623284700
If, on the other hand, from your comment.
My aim is to calculate the sum of the first 100 even numbers
Then you can do that like so
sumFirstNeven = (((2N + 2)N)/2 = (N+1)N
so (101)100 = 10100 and the complexity is O(1)
as I figured, you want a program to sum 100 first even values of the Fibonacci series.
here is a sample code, when you run the program it will ask you to determine the number of the even values, you want 100 value e.g, type 100 in consul:
public static void main(String[] args) {
int firstNumber = 0;
int secondNumber = 2;
System.out.print("Enter the number of odd elements of the Fibonacci Series to sum : ");
Scanner scan = new Scanner(System.in);
int elementCount = scan.nextInt(); // number of even values you want
System.out.print(firstNumber + ", ");
System.out.print(secondNumber + ", ");
long sum = 2;
for (int i = 2; i < elementCount; i++) {
int nextNumber = firstNumber + secondNumber;
System.out.print(nextNumber + ", ");
sum += (nextNumber);
firstNumber = secondNumber;
secondNumber = nextNumber;
}
System.out.print("...");
System.out.println("\n" + "the sum of " + elementCount + " values of fibonacci series is: " + sum);
}
Working on a problem in which I take a user's input (1-10) and guess what number they are thinking of using binary search, and update the range dependent on their answer (e.g. if it is greater than 5, I update the lowerLimit to 6) but am having trouble with the logic.
I use the middle cell as reference by adding 1 to the middle cell when they say it is greater than it, but I believe this is where I get confused. I can't figure out how to intertwine my if/else statement to update the number correctly.
Main method:
public class Main {
public static void main(String[] args) {
// test your program here
GuessingGame game = new GuessingGame();
game.play(1,10);
}
}
GuessingGame method (play method is the one I'm working with):
import java.util.Scanner;
public class GuessingGame {
private Scanner reader;
public GuessingGame() {
// use only this scanner, othervise the tests do not work
this.reader = new Scanner(System.in);
}
public void play(int LL, int UL) {
instructions(LL, UL);
int limit = howManyTimesHalvable(UL - LL);
int finalNumber = 0;
int midPoint = average(LL, UL);
int avgLL;
int avgUL;
for(int i = 0; i < limit; i++){
if(isGreaterThan(midPoint)){
midPoint++;
LL = midPoint;
midPoint = average(UL,LL);
finalNumber = LL;
}else{
midPoint--;
UL = midPoint;
midPoint = average(UL,LL);
finalNumber = LL;
}
if(UL == LL){
break;
}
}
System.out.println("Your number is : " + finalNumber);
}
public boolean isGreaterThan(int value){
System.out.println("Is your number greater than " + value + "?");
return reader.nextLine().equals("y");
}
public int average(int firstNumber, int secondNumber){
int total = firstNumber + secondNumber ;
return total / 2;
}
public void instructions(int lowerLimit, int upperLimit) {
int maxQuestions = howManyTimesHalvable(upperLimit - lowerLimit);
System.out.println("Think of a number between " + lowerLimit + "..." + upperLimit + ".");
System.out.println("I promise you that I can guess the number you are thinking with " + maxQuestions + " questions.");
System.out.println("");
System.out.println("Next I'll present you a series of questions. Answer them honestly.");
System.out.println("");
}
// a helper method:
public static int howManyTimesHalvable(int number) {
// we create a base two logarithm of the given value
// Below we swap the base number to base two logarithms!
return (int) (Math.log(number) / Math.log(2)) + 1;
}
}
I would like to know how to update the ranges accordingly, when a user says that the number that they've guessed is higher or lower than what is shown to them.
Edit, example entries:
Looking for number 9,
LL: 1
UL: 10
limit:4
finalNumber:0
midPoint:5
i: 0
Is your number greater than 5?
y
LL: 6
UL: 10
limit:4
finalNumber:6
midPoint:8
i: 1
Is your number greater than 8?
LL: 9
UL: 10
limit:4
finalNumber:9
midPoint:9
i: 2
Is your number greater than 9?
n
LL: 9
UL: 8
limit:4
finalNumber:9
midPoint:8
i: 3
Is your number greater than 8?
y
Your number is : 9
You should not have midPoint--;
Since you are asking the question as "is Grater than"? Your new upper limit should be midpoint if the answer is no.
I'm trying to write a code which will show the highest, lowest, the difference of them and the average of inputted 30 numbers.
But its not working and is showing the same result for both min and max numbers. Here is the code.
public class aa {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int[] daystemp = new int[30];
int i = 0;
int dayHot = 0;
int dayCold = 0;
while(i < daystemp.length){
daystemp[i] = input.nextInt();
i++;
}
int maxTemp = daystemp[0];
while (i < daystemp.length) {
if (daystemp[i] > maxTemp) {
maxTemp = daystemp[i];
dayHot = i + 1;
i++;
}
}
System.out.println(maxTemp);
int minTemp = daystemp[0];
while (i < daystemp.length) {
if (daystemp[i] < minTemp) {
minTemp = daystemp[i];
dayCold = i + 1;
i++;
}
}
System.out.println(minTemp);
int diff = maxTemp - minTemp;
System.out.println("The difference between them is"+diff);
double sum = 0;
while(i < daystemp.length) {
sum += daystemp[i];
i++;
}
double average = sum / daystemp.length;
System.out.println("Average was"+average);
}
}
After the first loop (the input loop), i value is daystemp.length (i.e. 30).
It's never reset to 0. So each while loop condition is false.
Add i=0 before the loops and do i++outside the ifblocks or your code will never end.
example:
i=0;
int maxTemp = daystemp[0];
while (i < daystemp.length) {
if (daystemp[i] > maxTemp) {
maxTemp = daystemp[i];
dayHot = i + 1;
}
i++;
}
A few notes about this solution:
By declaring the cumulative total double, no casting is required.
Because Java knows you want to convert int to double automatically if you assign an int to a declared double. Similary the fact that you want to express a result as double is implied when dividing a double by an int, such as when the average is taken. That avoids a cast also. If you had two ints and you wanted to produce a double you'd need to cast one or more of them, or in cases like a print statement where the compiler can't deduce the optimal type for the parameter, you'd need to explicitly cast to covert an int value to a double.
Not sure what OS you're running this on. The ideal situation would be to make it work on all platforms without requiring people type a magic word to end input (because how tacky). The easiest way to end input is to use the OS-specific end of input (end of file) key combination, and for Linux it's CTRL/D, which is how I explained it in the prompt. On another OS with a different end of input sequence you could just change the prompt. The trickiest would be if it is supposed to be truly portable Java. In that case I'd personally investigate how I could figure out the OS and/or End of File character or key combination on the current OS and modify the prompt to indicate to end input with whatever that is. That would be a bit of and advanced assignment but a very cool result.
Example illustrates use of a named constant to determine the array and is used limit the amount of input (and could be used to limit loop count of for loops accessing the array).
By setting the min and max to very high and low values respectively (notice the LOW value assigned to max and HIGH value assigned to min, those ensure the first legit temp entered will set the min and max and things will go from there).
Temperature Maximum, Minimum, Average and Difference Calculator
import java.util.Scanner;
public class TemperatureStats {
final static int MAX_DAYS = 31;
public static void main(String[] args) {
int[] dayTemps = new int[MAX_DAYS];
double cumulativeTemp = 0.0;
int minTemp = 1000, maxTemp = -1000;
Scanner input = new Scanner(System.in);
System.out.println("Enter temps for up to 1 month of days (end with CTRL/D):");
int entryCount = 0;
while (input.hasNextInt() && entryCount < MAX_DAYS)
dayTemps[entryCount++] = input.nextInt();
/* Find min, max, cumulative total */
for (int i = 0; i < entryCount; i++) {
int temp = dayTemps[i];
if (temp < minTemp)
minTemp = temp;
if (temp > maxTemp)
maxTemp = temp;
cumulativeTemp += temp;
}
System.out.println("High temp. = " + maxTemp);
System.out.println("Low temp. = " + minTemp);
System.out.println("Difference = " + (maxTemp - minTemp));
System.out.println("Avg temp. = " + cumulativeTemp / entryCount);
}
}
after adding two int variables to (a) and (b),
I have to gamble (a) times values between 10 to 100 and
calculate which square root of these gambled numbers is the closest to (b).
For example a=3 and b=2
output:
Gambled 16,25,49.
The number 16 was chosen since it's square root (4) is the closest to b=2.
I am stuck in the part of calculation of the square root, and saving the closest value to b each time the loop runs, i'm not allowed to use arrays,
this is the third question of my first task and i'd appreciate any experienced ideas to be shared ^^.
(MyConsole is a replacement for the scan command)
int a = MyConsole.readInt("Enter value a:");
int b = MyConsole.readInt("Enter value b:");
for(int i = 0; i<a; a--){
int gambler = ((int)(Math.random() *91)+10);
double Root = Math.sqrt(gambler);
double Distance= Root-b;
{
System.out.println();
Here is how I found the minimum. You can also use arrays to store all the gambling values if you want for future use. I also used Scanner but you can use your MyConsole I'm just not familiar with it. I hope it helps. דש מישראל
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter value a:");
int a = scanner.nextInt();
System.out.println("Enter value b:");
int b = scanner.nextInt();
double min = 100;
for (int i = 0; i < a; i++) {
int gambler = ((int) (Math.random() * 91) + 10);
double root = Math.sqrt(gambler);
double distance = Math.abs(root - b);
if (min > distance)
min = distance;
}
System.out.println("minimum value is: " + min);
}
This was part of my assignment and was asked to calculate factorial of 5 and 7.
I finished it as below:
import java.util.Scanner;
public class Factorial {
public static void main(String [] args)
{
System.out.println("Please enter a number: ");
Scanner input=new Scanner(System.in);
int number=input.nextInt();
int i,fact=1;
for(i=1;i<=number;i++){
fact=fact*i;
}
System.out.println("Factorial of " + number + " is: " + fact);
}
}
It worked for 5 and 7 (resulting 120 and 5040).
But my professor came over and test it with 20 and 987654321, result returns -2102132736 and 0.
Why is that?
P.S. I thought for the case of 987654321, the result would crush the application or return error since it would be huge.
This code can solve your problem . It is taken from here
class BigFactorial
{
static void factorial(int n)
{
int res[] = new int[300];
// Initialize result
res[0] = 1;
int res_size = 1;
// Apply simple factorial formula n! = 1 * 2 * 3 * 4...*n
for (int x=2; x<=n; x++)
res_size = multiply(x, res, res_size);
System.out.println("Factorial of given number is: ");
for (int i=res_size-1; i>=0; i--)
System.out.print(res[i]);
}
// This function multiplies x with the number represented by res[].
// res_size is size of res[] or number of digits in the number represented
// by res[]. This function uses simple school mathematics for multiplication.
// This function may value of res_size and returns the new value of res_size
static int multiply(int x, int res[], int res_size)
{
int carry = 0; // Initialize carry
// One by one multiply n with individual digits of res[]
for (int i=0; i<res_size; i++)
{
int prod = res[i] * x + carry;
res[i] = prod % 10; // Store last digit of 'prod' in res[]
carry = prod/10; // Put rest in carry
}
// Put carry in res and increase result size
while (carry!=0)
{
res[res_size] = carry%10;
carry = carry/10;
res_size++;
}
return res_size;
}
// Driver program
public static void main(String []args)
{
factorial(100);
}
}
Because 5040! is a very larger number (even long overflows). Use a BigInteger like
System.out.println("Please enter a number: ");
Scanner input = new Scanner(System.in);
int number = input.nextInt();
BigInteger fact = BigInteger.ONE;
for (int i = 2; i <= number; i++) { // <-- x * 1 = x
fact = fact.multiply(BigInteger.valueOf(i));
}
System.out.println("Factorial of " + number + " is: " + fact);
This is because of the fact that the container that you have taken for storing and printing your result does not have the capacity to hold such big integer (I mean factorial of 20). So, you need a bigger container. As others already suggested, you can use BIGINTEGER.