Java While loop to generate sequence 1, 2, 0, 3, -1, 4 - java

I am working my way through a beginners java book which contains some practice questions to try out. I am currently learning while loops and there is one question that I am stuck on and have been for the last two days.
It is asking me to use a single While loop to print out the first ten numbers in the sequence 1, 2, 0, 3, -1, 4.
I have worked out the easy bit which is how the sequence goes( add 1, take 2, add 3, take 4, add 5, take 6, add 7 etc.) However I have no idea how to implement this.
public class WhileTester {
public static void main(String[] args) {
System.out.println("First 10 numbers in the sequence 1, 2, 0, 3, -1, 4, -2 ...");
int i = 0;
while (i <= 6) {
int a = 1;
int num = i;
if (i % 2 == 0) {
num = -i;
} else {
num = i;
}
a = a + num;
System.out.print(a+ ", ");
i++;
I know this is no where near close to where I need to get to but I am stuck for ideas.
Thanks in advance.

I think you got the sequence wrong, look at the numbers:
It starts at 1, then +1, -2 +3 -4 +5 ...
Maybe that helps on your implementation.

You set a back to 1 inside the loop with each iteration. Shouldn't a be initialized outside the loop?

It strikes me that you are a lot closer to a solution than you give yourself credit for.
if you take the num and move it outside the loop....
then, you add 1 to num each time you loop:
On 'even' loops you add num, and on odd loops you subtract num. Now, you need to loop 10 times (not until the value is 6.... which is a 'coincidence'....):
Your critical code becomes:
int num= 0;
int a = 1;
for (int i = 0; i < 10; i++) {
System.out.println(a);
num++;
if (i % 2 == 0) {
a += num;
} else {
a -= num;
}
}

public class tester {
public static void main(String[] args) {
System.out.println("First 10 numbers in the sequence 1, 2, 0, 3, -1, 4, -2 ...");
int num = 0;
int a = 1;
int value = 0;
while (value <= 10) {
if (value % 2 == 0) {
num = -value;
} else {
num = value;
}
a = a + num;
System.out.print(a + ", ");
value++;
}
}
}

class can't be compiled for the program:-
public class WhileTester {
public static void main(String[] args) {
System.out.println("First 10 numbers in the sequence 1, 2, 0, 3, -1, 4, -2 ...");
int i = 0;
while (i <= 6) {
int a = 1;
int num = i;
if (i % 2 == 0) {
num = -i;
} else {
num = i;
}
a = a + num;
System.out.print(a+ ", ");
i++;
it is a wrong information

Related

is there some solution to reduce the time complexity of a program in java?

The question is
Given a number 'N'. The task is to find the Nth number whose each
digit is a prime number(<10) i.e 2, 3, 5, 7. In other words you have
to find nth number of this sequence : 2, 3, 5, 7, 22, 23 ,.. and so
on.
I'm trying the below code which exceeds the time bound.
import java.io.*;
import java.util.*;
class Main {
public static boolean AlldigitsPrime(int m){
for(; m>0;){
int dig=m%10;
if(dig!=2 && dig!=3 && dig!=5 && dig!=7){
return false;
}
m/=10;
}
return true;
}
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
for(int i=0; i<t; i++){
int n=sc.nextInt();
int count=0;
for(int j=2; j>=2 ; j++){
if(AlldigitsPrime(j)){
count++;
if(count==n){
System.out.println(j);
break ;
}
}
}
}
}
}
You can calculate the n-th number almost directly. The idea is to convert n into a new number system in which no number contains a 0 digit (apart from the leading ones which are ignored) but consists only of digits 1 to 4. That is: 1 -> 1, 4 -> 4, 5 -> 11, 6 -> 12, 9 -> 21 and so on. The last step is to replace each digit (1 to 4) by its correspondig prime number (2, 3, 5 or 7).
Although I usually refrain from giving example code for homework, but because it's a bit complicated, I'll show the solution:
public int convert(int n) {
if (n < 1) throw new IllegalArgumentException("n must be positive but was " + n);
int[] primes = {2,3,5,7};
int result = 0;
int power = 1;
do {
int digit = (n-1) % 4 + 1; // we don't want 0es
result += primes[digit - 1] * power;
power *= 10;
n = (n-1) / 4; // take the 'removed' 0es into consideration
} while (n > 0);
return result;
}
Of course, addition and subtraction of 1 in the first two lines of the loop is not necessary but I kept it there to highlight the 'removal' of 0es while calculating the digits.

Print optimal solution for coin change algorithm

Given a value N, if we want to make change for N cents, and we have infinite supply of each of S = { S1, S2, .. , Sm} valued coins, what is the optimal way to make change for N cents.
Example:
S = {2, 5, 10}
N = 6, then optimal solution is : 2, 2, 2
I have below working code:
public static void main(String argv[]) {
long n = 10L;
int[] combo = new int[100];
int[] amounts = { 2, 5, 10 };
ways(n, amounts, combo, 0, 0, 0);
}
public static void ways(long n, int[] amounts, int[] combo, int startIndex, int sum, int index) {
if (sum == n) {
printArray(combo, index);
}
if (sum > n) {
return;
}
for (int i = 0; i < amounts.length; i++) {
sum = sum + amounts[i];
combo[index] = amounts[i];
ways(n, amounts, combo, startIndex, sum, index + 1);
sum = sum - amounts[i];
}
}
public static void printArray(int[] combo, int index) {
for (int i = 0; i < index; i++) {
System.out.print(combo[i] + " ");
}
System.out.println();
}
Output:
2 2 2 2 2
5 5
10
Here I just need to optimal combination with less number of coins so only 10 in this example code.
But this code uses recursive approach, my value for N is Long type so as the value of N increases I am getting stackoverflow error.
The recursive approach I am following here is not correct, What is the correct way to solve this problem?
Update:
Based on MBo answer I tried below program, but I am not able to get the correct results:
static void testcase() {
// make int array A of size N+1
int N = 6;
int[] A = new int[N + 1];
// make int array P of size N+1
int[] P = new int[N + 1];
// fill A[] with large value (len(S) + 1)
int[] S = { 2, 5, 10 };
int lengthOfS = S.length;
for (int i = 0; i < A.length; i++) {
A[i] = lengthOfS + 1;
}
A[0] = 0;
for (int s : S) {// coin value
for (int i = s; i <= N; i++) {
if (A[i - s] < A[i] + 1) { // using this coin we can get better
// result for sum i
A[i] = A[i - s] + 1;
P[i] = s; // write down coin for this sum
}
}
}
System.out.println(Arrays.toString(P)); // [0, 0, 2, 2, 2, 5, 2]
System.out.println(A[N]);// 3
int idx = N;
for (int i = 0; i < A[N]; i++) {
int result = idx - P[idx];
System.out.println(result); // 4 2 0
idx = result;
}
}
This code prints:
[0, 0, 2, 2, 2, 5, 2]
3
4
2
0
How to fix this code?
For fixed set S = {2, 5, 10} solution is rather simple:
No solutions for N=1,3
if N is odd, you must use 5 - so N=N-5
Now use greedy approach: get as much 10-s as possible, then as much 2-s as possible
def best(N):
print(N, end = ": ")
if (N % 2):
print("5", end = " ")
N = N - 5
if N >= 10:
print("10*", N//10, end = " ")
N = N % 10
if N > 1:
print("2*", N//2, end = " ")
21: 5 10* 1 2* 3
22: 10* 2 2* 1
23: 5 10* 1 2* 4
24: 10* 2 2* 2
In general you can find optimal solution using dynamic programming.
The first way is "memoization" - you have to implement recursive approach with the choice of the best solution, then add storing intermediate results in hashmap or another structure. Simple implementation:
S = [2, 3, 5, 7, 11]
dic = {}
def rec(summ):
if summ == 0:
return 0
rd = dic.get(summ)
if rd != None:
return rd
minn = 9999999999999
for s in S:
if s <= summ:
minn = min(minn, 1 + rec(summ - s))
dic[summ] = minn
return minn
N = 1000
print(rec(N))
>>92
Another way is using table - you fill it with the best possible results using the first item, then update solution using the second item and so on.
Pseudocode
make int array A of size N+1
make int array P of size N+1
fill A[] with large value (MaxInt` or at least `N/min(S))
A[0] = 0
for s in S: //coin value
for (i = s; i <= N; i++)
if A[i - s] < A[i] + 1 //using this coin we can get better result for sum i
A[i] = A[i - s] + 1
P[i] = s //write down coin for this sum
Now we have A[N] with the best count, and can retrieve needed coins using P[N], P[N - P[N]]... sequence.
Working Python code
S = [2, 3, 5, 7, 11]
N = 17
A = [0] + [10000] * N
P = [0] * (N + 1)
for s in S: #coin value
for i in range(s, N + 1):
if A[i - s] < A[i] + 1: #using this coin we can get better result for sum i
A[i] = A[i - s] + 1
P[i] = s #write down coin for this sum
print(A) #for reference
i = N
while i > 0:
print(P[i], end = " ")
i = i - P[i]
>> [0, 10000, 1, 1, 2, 1, 2, 1, 2, 2, 2, 1, 2, 2, 2, 3, 2, 3]
>> 11 3 3
Note - if we can use every coin only once, we have to make inner loop in backward direction to avoid multiple adding the same coin
As the number of coins is small, and since the amount can be large, it is likely that backtracking can provide a good solution
Here is an implementation in C++
(Note: this code was already posted, but I can't find the post. Question deleted ?)
The coins are first sorted if descending order, to fasten the search.
In order to minimize the number of coins, we first try solutions with maximum possible number of coins of largest value.
In a given search, if the current number of coins is larger than the current minimum number of coins, we stopped the search ("premature abandon").
In the code, "UP" means that we will consider adding coins with a lower value
"DOWN" means that we will try to decrease the number of coins of higher value.
At a given step, we maintain an array corresponding to the number of coins for each coin value
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
// The order of array coins is modified
std::vector<int> get_change(std::vector<int>& coins, int amount) {
std::vector<int> n_coins(coins.size(), 0);
std::vector<int> n_coins_opt(coins.size(), 0);
int n = coins.size();
std::sort(coins.begin(), coins.end(), std::greater<int>());
int sum = 0; // current sum
int i = 0; // index of the coin being examined
int n_min_coins = amount / coins[n - 1] + 1;
int n_total_coins = 0;
bool up_down = true;
while (true) { // UP
if (up_down) {
n_coins[i] = (amount - sum) / coins[i]; // max possible number of coins[i]
sum += n_coins[i] * coins[i];
n_total_coins += n_coins[i];
if (sum == amount) {
if (n_total_coins < n_min_coins) {
n_min_coins = n_total_coins;
n_coins_opt = n_coins;
}
up_down = false;
sum -= n_coins[i] * coins[i];
n_total_coins -= n_coins[i];
n_coins[i] = 0;
i--;
}
else {
if (i == (n - 1) || (n_total_coins >= n_min_coins)) { // premature abandon
sum -= n_coins[i] * coins[i];
n_total_coins -= n_coins[i];
n_coins[i] = 0;
up_down = false;
i--;
}
else {
i++;
}
}
}
else { // DOWN
if (i < 0) break;
if (n_coins[i] == 0) {
if (i == 0) break;
i--;
}
else {
sum -= coins[i];
n_coins[i] --;
n_total_coins--;
i++;
up_down = true;
}
}
}
return n_coins_opt;
}
int main() {
std::vector<int> coins = {2, 5, 10};
int amount = 1731;
auto n_coins = get_change(coins, amount);
int sum = std::accumulate (n_coins.begin(), n_coins.end(), 0);
if (sum == 0) {
std::cout << "no solution\n";
} else {
std::cout << amount << " = ";
for (int i = 0; i < n_coins.size(); i++) {
std::cout << n_coins[i] << "*" << coins[i] << " ";
}
std::cout << "\n";
}
return 1;
}

Program that compute the multiplication table for all numbers less than or equal N in JAVA

How do I Write a program that compute the multiplication table for all numbers less than or equal N. Note that N is an integer read from the user.
The program will repeatedly
do that until the user enter -1 in JAVA.
I don't know if i should use nested loops or a method for this, but I wrote the following uncompleted code which is giving me an infinite loop
public static void main(String[] args) {
int N ;
System.out.println("Enter N: " );
N = in.nextInt();
while ( N != -1) {
for(int i = 1; i <= N; ++i)
{
for (int c = 1; c <= 10; ++c)
System.out.println(N + "*" + c + " = " + (N*c));
}
}
}
I want an output like this :
Enter an integer to print it's multiplication table, -1 to
exit
2
Multiplication table of 1
1*1 = 1, 1*2 = 2, 1*3 = 3, 1*4 = 4, 1*5 = 5, 1*6 = 6, 1*7 =
7, 1*8 = 8, 1*9 = 9, 1*10 = 10,
Multiplication table of 2
2*1 = 2, 2*2 = 4, 2*3 = 6, 2*4 = 8, 2*5 = 10, 2*6 = 12, 2*7
= 14, 2*8 = 16, 2*9 = 18, 2*10 = 20,
Enter an integer to print it's multiplication table, -1 to
exit
-1
Sunny Patel's answer is correct, but just to show you another way to do it:
import java.util.Scanner;
import java.util.stream.IntStream;
public class Multiply {
public static void main(String [] args) {
try (Scanner in = new Scanner(System.in)) {
int N;
do {
System.out.println("Enter N: " );
N = in.nextInt();
IntStream.range(1, N+1)
.forEach(i -> IntStream.range(1, 11)
.forEach(j -> System.out.println(String.format("%d*%d = %d", i, j, (i*j)))));
} while ( N != -1);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Your code runs in an infinite loop because N does not change within the outer for-loop.
You can place the prompt inside the loop, and change to a do-while loop to guarantee at least 1 execution; or none if the user enters a number less than 1 (because of the outer for-loop).
You were also missing reference to Scanner to capture the input.
Finally, you forgot to use i instead of N in the output, otherwise the inner loop will output the same values every time.
import java.util.Scanner; // Import Scanner
public static void main(String[] args) {
int N;
Scanner in = new Scanner(System.in); // Missing Scanner
do { // Changed to do-while loop
System.out.println("Enter N: " );
N = in.nextInt(); // Prompt user for N.
for(int i = 1; i <= N; ++i)
{
for (int c = 1; c <= 10; ++c)
System.out.println(i + "*" + c + " = " + (i*c)); // Use i instead of N
}
} while ( N != -1);
}

Hailstone Program in Java

I have the following program to write:
An interesting (yet unsolved) question in mathematics is called "hailstone numbers". This series is produced by taking an initial integer and if the number is even, dividing it by 2. If the number is odd, multiply it by 3 and add 1. This process is the repeated.
For example: An initial number of 10 produces: 10, 5, 16, 8, 4, 2, 1, 4, 2, 1... An initial value of 23 produces: 23, 70, 35, 106, 53, 160, 80, 40, 20, 10, 5, 16, 8, 4, 2, 1, 4, 2, 1...
Note that both numbers eventually reach the 4, 2, 1, 4, 2, 1... cycle.
Create an application that offers the user three different ways to run this program.
Option 1: Print the hailstone numbers for a single entry and its length
Example: Input> 10 10, 5, 16, 8, 4, 2, 1 Length 7
Option 2: Print all of the hailstone numbers from 4 to a given entry
Example: Input> 6 4, 2, 1 Length 3 5, 16, 8, 4, 2, 1 Length 6 6, 3, 10, 5, 16, 8, 4, 2, 1 Length 9
Option 3: Print out the number with the maximum number of iterations need to reach the cycle and which starting number produces this maximum from 4 to the number entered.
Example: Input> 6 Longest: 6 Length: 9
In writing this program you must implement the following method...
/**
*
* #param num Number that a hailstone chain will be generated
* #param showNumbers true if list of numbers is shown to screen
* #return Count of the numbers in the num hailstone chain.
*/
private static int hailStone(int num, boolean showNumbers) {
// your code
}
This is the code I've written so far:
public static void main(String[] args) {
int a = getInt("Give a number: ");
System.out.print("How would you like to run the program? Option 1 prints hailstone numbers for a single entry and its length." +
"Option 2 prints all the hailstone numbers from 4 to a given entry. Option 3 prints the number with the maximum number" +
"of iterations needed to reach the 4, 2, 1 cycle.");
int option = console.nextInt();
boolean showNumbers = (option == 1 || option == 2);
hailStone(a, showNumbers);
}
public static int getInt(String prompt) {
int input;
System.out.print(prompt);
input = console.nextInt();
return input;
}
private static void hailStone (int a, boolean showNumbers) {
if (showNumbers == true) {
if (a % 2 == 0) {
for (int i = 0; i < 50; i++) {
for (int j = 0; j <= i; j++)
a /= 2;
System.out.print(a + " ");
a *= 3;
a += 1;
System.out.print(a + " ");
}
} else {
for (int i = 0; i != a; i++) {
}
}
} else {
}
}
I feel like I've hit a brick wall because I have no idea how to implement all these options in the method my teacher is requiring us to use. Plus, I can't seem to get even the basic hailstone chain to print. Help?
The HailStone algorithm should not be hard to implement. It will actually be much easier if you make it a recursive function, since that is more natural Writing it as an iterative function is probably what is causing your issues.
This should be enough to get you started, this is a working HailStone implementation using a recursive function. You can implement the rest of the project requirements quite easily once you've got the algorithm working... but I'd like to challenge you to convert this into a working iterative function once you get the features correct and to write unit tests to test the program. (TDD dictates that you should write your tests BEFORE you write the actual implementation. This is a great practice that is often skipped due to time constraints and the perception that a strong test suite is overkill.)
HailStone.java
public class HailStone {
/* static variable to count calls to hailStone */
public static int iterCount = 0;
/* This variable is a senti */
public static boolean isRepeating = 0;
/* Simple main function */
public static void main(String[] args) {
// TODO:
// Either parse args or use a scanner to get input.
// Args = verbose, entryPoint
hailStone(10, true);
}
/* Recursive hailStone implementation */
private static void hailStone(int a, boolean showNumbers) {
// start off by printing the numbers if showNumbers is true
if (showNumbers) {
System.out.printf("Iteration #%d: %d\n", ++iterCount, a);
}
// base case: a = 1 => most important part of recursion
if (a == 1) {
if (isRepeating) {
return;
}
isRepeating = true;
}
// check if a is odd
// You can use modulo divison, but we'll use bitwise &
/* Explained: [ bitwise AND... bits that are set in a AND in 1 ]
**********************************************
Case 1: a is even =>
a = 10
10 in binary is 00001010
1 in binary is 00000001
------------------------------
10 & 1 in binary is 00000000
Case 2: a is odd =>
a = 10
11 in binary is 00001011
1 in binary is 00000001
------------------------------
11 & 1 in binary is 00000001
**********************************************
set(X) = set of all even numbers
set(Y) = set of all odd numbers
{
x is any arbitrary number in set X,
y is any arbitrary number in set Y
}
x & 1 will ALWAYS equal 0 -\
>- know this. bitwise hacks rock.
y & 1 will ALWAYS equal 1 -/
*/
if ((a & 1) == 1) {
a *= 3;
a += 1;
} else {
a /= 2;
}
// Tail recursion.
hailStone(a, showNumbers);
return;
}
}
without all the comments and extra stuff:
public class HailStone {
public static int iter_count = 0;
public static void main(String[] args) {
hailStone(10, true);
}
/* Recursive hailStone implementation */
private static void hailStone(int a, boolean showNumbers) {
if (showNumbers) {
System.out.printf("Iteration #%d: %d\n", ++iter_count, a);
}
// base case: a = 1
if (a == 1) {
return;
}
if ((a & 1) == 1) { // a is odd:
a *= 3;
a += 1;
} else {
a /= 2;
}
hailStone(a, showNumbers);
return;
}
}
private static Scanner console = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("How would you like to run the program?");
System.out.println(" [1] - print hailstone numbers for a single entry and its length.");
System.out.println(" [2] - print all hailstone numbers from 4 to a given entry.");
System.out.println(" [3] - print the number with the maximum number of iterations needed to reach the 4, 2, 1 cycle.");
int option = queryInt("Option: ", 1, 3);
switch (option) {
case 1: {
int seed = queryInt("INPUT> ", 1, Integer.MAX_VALUE);
hailStone(seed, true);
break;
}
case 2: {
int maxSeed = queryInt("INPUT> ", 4, Integer.MAX_VALUE);
for (int i = 4; i <= maxSeed; i++) {
hailStone(i, true);
}
break;
}
case 3: {
int maxSeed = queryInt("INPUT> ", 4, Integer.MAX_VALUE);
int longestChain = 0;
int longestChainLength = 0;
for (int i = 4; i <= maxSeed; i++) {
int length = hailStone(i, false);
if(length > longestChainLength) {
longestChain = i;
longestChainLength = length;
}
}
System.out.println("Longest: " + longestChain + " Length: " + longestChainLength);
break;
}
}
}
private static int queryInt(String prompt, int min, int max) {
while (true) {
System.out.print(prompt);
String input = console.nextLine();
try {
int result = Integer.parseInt(input);
if (result >= min && result <= max) {
return result;
} else {
System.err.print("Expected a number ");
if (min == Integer.MIN_VALUE) {
System.err.println(" less than or equal to " + max);
} else if (max == Integer.MAX_VALUE) {
System.err.println(" greater than or equal to " + min);
} else {
System.err.println(" between " + min + " and " + max);
}
}
} catch (NumberFormatException ex) {
System.err.println("Not a number: " + input);
}
}
}
private static int hailStone(int num, boolean showNumbers) {
int result = 1;
for (Iterator<Integer> chain = iterateHailStone(num); num != 1; num = chain.next(), result++) {
if (showNumbers) {
System.out.print(num + ", ");
}
}
if (showNumbers) {
System.out.print(num);
System.out.println(" (length=" + result + ")");
}
return result;
}
private static Iterator<Integer> iterateHailStone(int seed) {
return new Iterator<Integer>() {
int value = seed;
#Override
public boolean hasNext() {
return true;
}
#Override
public Integer next() {
if (value % 2 == 0) {
value /= 2;
} else {
value *= 3;
value++;
}
return value;
}
};
}

Euler Project 2

So I am not very good at it yet at all (understatement). I am trying to solve problems in the Euler project, and I am already stuck on 2.
Each new term in the Fibonacci sequence is generated by adding the previous 2 terms. By starting with 1 and 2, the first 10 terms will be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
Here is my code which I have repeatedly tried to fix:
(I think there is something wrong with the for loop logic.)
public class tesy {
public static void main(String args[]) {
int fib = 0;
int tot = 0;
int total = 0;
for (fib = 0; tot < 4000000; fib++) {
tot = fib + (fib + 1);
if (tot % 2 == 0) {
total = tot + total;
}
}
System.out.println(total);
}
}
Your logic is erroneous in couple of ways,
tot = fib + (fib + 1); /** This will always be `(2*fib + 1)` and `fib` is getting
incremented by 1 each time. You have no reference to the previous two terms of the
sequence. **/
Try the below logic instead.
class Fibonacci
{
public static void main (String[] args)
{
int fiboFirst = 1;
int fiboSecond =2;
int fib = 0;
int sum = 0;
while(fiboSecond < 4000000)
{
// This will calculate the current term of the sequence
fib = fiboFirst + fiboSecond;
// Below two lines will update fib[i] and fib[i - 1] terms
// for the next loop iteration.
fiboFirst = fiboSecond; // fib[i]
fiboSecond = fib; // fib[i -1]
if (fib % 2 == 0)
{
sum = sum + fib;
}
}
System.out.println(sum+2);
}
}
Explanation
Here fiboFirst is equivalent to F[n] and fiboSecond is equivalent
to F[n - 1] in the Fibonacci sequence definition. In each iteration,
those two values should be replaced, in order to be used in the next
iteration. That is why I have these two lines,
fiboFirst = fiboSecond; // fib[i]
fiboSecond = fib; // fib[i -1]
HERE is the execution of the above program
You don't seem to be following the actual equation used to generate a fibonacci sequence, therefore there is no (obvious) way of fixing your code.
int fibA = 1, fibB = 2, total = 0;
while(fibB <= 4000000) {
// Add to the total, set fibA to fibB and get the next value in the sequence.
if(fibB % 2 == 0) total += fibB;
int temp = fibA;
fibA = fibB;
fibB = fibB + temp;
}
The above code should find the sum of all values less than or equal to 4000000
Here is a solution that uses BigInteger. Please verify the results.
public class Fibonacci{
public static void main(String[] args) {
BigInteger r = fibonacciEvenSum();
System.out.println(r);
}
public static BigInteger fibonacciEvenSum(){
int f = 1;
int s = 2;
int mn4 = 4000000;
BigInteger sum = BigInteger.valueOf(0);
while(s <= mn4){
if(s % 2 == 0){
sum = sum.add(BigInteger.valueOf(s));
}
f = f + s;
s = s + f;
}
return sum;
}
}
Before writing a program like this, you should first think of what's underlying this program. You should first understand how to generate a Fibonacci series before graduating on to doing something with the series. I'll give you my solution so that you can understand.
class euler2 {
public static void main(String[] args) {
int a = 0, b = 1; /* the first elements of Fibonacci series are generally
thought to be 0 and 1. Therefore the series is 0, 1, 1, 2, 3... .
I've initialized first and second elements such */
double sum = 0; // The initial sum is zero of course.
while (b < 4000000) /* since b is the second term, it will be our control variable.
This wouldn't let us consider values above 4M. */
{
int ob = b; // to swap the values of a and b.
b = a + b; // generating next in the series.
a = ob; // a is now the older value of b since b is now a + b.
if (b % 2 == 0) // if b is even
sum += b; // we add it to the sum
}
System.out.println(sum); // and now we just print the sum
}
}
Hope this helped!

Categories