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;
}
Related
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);
}
Good evening,
My and my bud tried to figure out why the program won't bubble sort the names that we input, maybe someone could hint about it.
public static void sortDatPlane(String Ref[]){
int n = Ref.length;
int k = 1;
int j = n - 2;
int i;
while(k < n){
i = 0;
while (i <= j) {
if(notInOrder(Ref, i, i+1)){
swap(Ref, i, i+1);
}
i++;
}
k++;
}
for (String Ref1 : Ref) {
System.out.println(Ref1);
}
}
public static void swap(String Ref[], int i, int j){
String temp = Ref[i];
Ref[i] = Ref[j];
Ref[j] = temp;
}
public static boolean notInOrder(String Ref[],int i, int j){
return Ref[i].substring(0,1).compareTo(Ref[j].substring(0,1)) == 1;
}
As stated by Ken Y-N in the comments, you are only comparing the first characters of the strings (substring(0, 1) does this). Remove that part and it will probably work.
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));
}
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.
Project Euler, problem 2: Determine the sum of the even numbers in the Fibonacci sequence up to 4 000 000. First I tried to
use a recursive algorithm for the sequence but I realized I dont have all the time in the world so I now use an iterative. It is still
extremely slow. Can I improve my code?
public class Euler2Correct {
public static int Fibonacci(int j){
/**
* Metod for returnerning number [I]j[/I] in the sequence.
*
*/
if(j<=1){
return 1;
}
else if(j==2){
return 2;
}
int tmp;
int a=2;
int b=1;
for(int k=3; k<=j; k++){
tmp=a+b;
b=a;
a=tmp;
}
return a;
}
public static void main(String[]args){
int s=0;
for(int i=2; i<4000000; i=i+3){ //Every three number is even
s = s + Fibonacci(i);
}
System.out.println(s);
}
}
You are recalculating the Fibonacci number over and over again from scratch. If you keep the running total in the Fibonacci method, you can use the previous results to prevent doing all that extra work.
Here's the modified version of your code, I tried to keep it as similar as possible
public class Euler2Correct {
public static int Fibonacci(int j) {
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));
}
}