How to get Fibonacci number with recursion - java

public class Fibonacci {
int fab=0;
int fab1=1;
public int fabi(int n){
if(n>=1){
System.out.println(n);
return fabi(fab-1)+fabi(fab1-2);
}
else
return 1;
}
}
I have some logical issue in code when I am using recursion method.

The function :
int fib(int n)
{
if (n <= 1)
return n;
return fib(n-1) + fib(n-2);
}

Related

Finding if a number is a power of another number recursivly

Consider this code
public static boolean isPower(int x, int n) {
if (x == 1)
return (n == 1);
int pow = 1;
while (pow < n)
pow = pow * x;
return (pow == n);
}
The goal is to find if the number x is a power of the number n.I've come up with this algorithm,and it works.I'd like to solve it recursivly as well.I read through the linked post someone put in the coments(https://softwareengineering.stackexchange.com/questions/279004/general-way-to-convert-a-loop-while-for-to-recursion-or-from-a-recursion-to-a)
I tried to replicate,or use the pattern that was provided in the answers.
First I tried to identify what my header,condition loop and tail was.
public static boolean isPower(int x, int n) {
if (x == 1)//header
return (n == 1);//header
int pow = 1;//header
while (pow < n)//condition
pow = pow * x;//loop
return (pow == n);//tail
}
Now I tried to apply the pattern;
public static boolean isPower_recursive(int x,int n) {
if(x == 1)
return (n==1);
return isPower_recursion( x, n, 1);
}
public static boolean isPower_recursion(int x,int n, int pow) {
if( 1 > n) {
return (pow == n);
}
pow = pow * x;
return isPower_recursion(x,n,pow);
}
This only works for the case when both x and n are one,in every other case I get a Stackoverflow error.The compiler says the error happens at the return statement in the isPower_recursive method,which leads me to think that i am not calculating this right.Some insight would be great.
I've solved the problem.I applied the patter wrong,it should look like this.
public static boolean isPower_recursive(int x,int n) {
if(x == 1)
return (n==1);
return isPower_recursion( x, n, 1);
}
public static boolean isPower_recursion(int x,int n, int pow) {
if( pow >= n) {
return (pow == n);
}
pow = pow * x;
return isPower_recursion(x,n,pow);
}

Recursive via Binary Search Help(Java)

We was told to make a binary search with the class Recursion. However I'm stuck as the recursion isn't working properly according to my professor(Didn't elaborate when I asked for help) and been working with a fellow student. We come to the conclusion of needing int count but I'm not sure where or how to implement it. Java isn't my strongest language so a guide or hint would be very helpful.
public class Recursive {
public int BinarySearch(int x[], int target, int low, int high)
{
if (low >= high) return -1;
int mid = (low + high)/2;
if (x[mid] > target)
return BinarySearch(x, target, low, mid-1);
else if (x[mid] < target)
return BinarySearch(x, target, mid+1, high); ;
return mid;
}
public int firstNnumber(int n)
{
if (n < 1) return 0;
return firstNnumber(n-1) + n;
}
public int firstNnumber2(int n)
{
if (n==1) return 1;
if (n==2) return 3;
boolean even = (n%2 == 0);
n /= 2;
if (even)
{
return 2*firstNnumber2(n) + n*n;
}
else
return 2*firstNnumber2(n) + (n + 1)*(1+n);
}
public int gaussian(int n)
{
return n*(n+1)/2;
}
static public void main(String [] args)
{
Recursive r = new Recursive();
System.out.println("By Gussain, Sum of first 100000 integers=" + r.gaussian(10000));
System.out.println("By recurssion 2, Sum of first 100000 integers=" + r.firstNnumber2(6));
}
}
This is what is printed, I don't understand what's the problem with my code.
By Gussain, Sum of first 100000 integers=50005000
By recurssion 2, Sum of first 100000 integers=21
your are calling with the wrong params, try calling with
static public void main(String [] args){
Recursive r = new Recursive();
System.out.println("By Gussain, Sum of first 100000 integers=" + r.gaussian(10000));
System.out.println("By recurssion 2, Sum of first 100000 integers=" + r.firstNnumber2(10000));
}

Recursive function to return the average of the digits in a number

I'm trying to create a recursive function that returns the average of the digits in a number. For example the average of the number 123 is 2.
I know how to write a function that sums the digits.
public static int sum (int n) {
if (n<10)
return n;
return n%10 + sum(n/10);
}
I also know how to count the digits
public static int numCount(int n) {
if (n<10)
return 1;
return 1 + numCount(n/10);
}
However I can't figure out how to calculate the average without using pre existing functions.
You can recursively iterate the array while keeping both accumulative sum and an index that shows which items were already iterated:
public class MyClass {
public static void main(String args[]) {
int[] arr = {1,2,3};
System.out.println(avg(arr)); // 2.0
}
private static double avg(int[] arr) {
return avg(arr, 0, 0);
}
private static double avg(int[] arr, int index, int sum) {
if (index == arr.length) {
return (double) sum / index;
}
return avg(arr, index + 1, sum + arr[index]);
}
}
Demo
Try this:
int recursive(int num, int startingSize) {
if(num < 10){
return num;
}
num = num % 10 + recursive(num/10, startingSize++);
return num/startingSize;
}
and for example : recursive(123, 1)
count=0;
public static int sum (int n) {
count++;
if (n<10)
return n;
return n%10 + sum(n/10);
}
double average = (double)sum(123)/count;
System.out.println("average:"+ average);
I mean if we are just talking numbers we don't even need recursive functions here
String s = Double(10.45).toString();
Int size = s.length();
int count = 0;
Int sum = 0;
for (int i = 0; i < size; I++ ) {
try {
sum += Integer.valueOf(s[i]);
++count;
} catch (Exception e) {}
}
return sum / count;
That should give you an. Average regardless of number, whole or real.

Java - Recursion sum of number and how it work

I am trying to write a recursive function that when I call with number 5 for example then the function will calculate the sum of all digits of five.
1 + 2 + 3 + 4 + 5 = 15
The current code always returns 0, how can the amount each time the n?
public class t {
public static void main(String[] args) {
System.out.println(num(5));
}
public static int num(int n) {
int sum = 0;
sum += n;
if (n == 0)
return sum;
return num(n - 1);
}
}
thank you.
Instead of setting the sum to 0 you can -
Do this:
public int sumUp(int n){
if (n==1)
return 1;
else
return sumUp(n-1)+n;
}
The problem is you set the sum always 0.
public static void main(String[] args) {
System.out.println(num(5, 0));
}
public static int num(int n, int sum) {
if (n == 0) {
return sum;
}
sum += n;
return num(n - 1, sum);
}
public static int withRecursion(List<Integer> list) {
int size = list.size();
int a=0;
if(list.isEmpty() == true) {
return 0;
}else {
a = a + list.get(0) + withRecursion(list.subList(1, size));
return a;
}
}

iterative program on java

i have this program
public static int p(int n, int m){
if(n==m) return n;
if (n<m) return p(n,m-n);
else return p(n-m,m);
}
how to put this program on iterative program with while loop.
Thanks
This code substacts the smaller of the two inputs from the larger until they are equal. This can be done with a while loop:
public static int p(int n, int m){
while (m!=n) {
if (n<m)
m -= n;
else
n -= m;
}
return n;
}

Categories

Resources