Farveaz
Farveaz

Reputation: 608

Printing prime numbers in Java using recursion

I wrote a similar function in C, and was able to achieve the required result unlike java. Below is the code, which checks if a number is prime recursively. Compilation says, i'm missing a return statement. The number to be checked if a prime is x. The variable i is the divisor.(ie) x/2, (x/2)-1,...0.

public int primes(int x, int i)
{
    if(i==0)
        return 1;
    if(x%i==0)
        return 0;
    else
        primes(x, i-1);
}

What is the complexity of this code if I had to print the first 1000 prime numbers.

Upvotes: 2

Views: 32087

Answers (7)

kirti
kirti

Reputation: 4609

You can apply the logic as:

for (i = 1; i <= 100; i++) {              
    int counter=0;    
    for (num = i; num>=1; num--) {
        if (i%num==0) {
            counter = counter + 1;
        }
    }
    if (counter == 2) {
        //Appended the Prime number to the String
        primeNumbers = primeNumbers + i + " ";
    }   
}

Then display primeNumbers.

Upvotes: 0

Tanvir Arafat
Tanvir Arafat

Reputation: 41

import java.util.Scanner;

public class Primenumm {
     public static void main(String[] args) {
          System.out.println("Enter the Number :-");
          Scanner s = new Scanner(System.in);
          int limit = s.nextInt();
          for (int i = limit; i >= 1; i--) {
               if (primes(i, i - 1) == 1) {
                    System.out.println(i + " is a prime no");
               } else {
                    System.out.println(i + " is not a prime no");
               }
          }
     }

     private static int primes(int x, int i) {
          if (i == 1) {
               return 1;
          }
          try {
               if (x % i == 0) {
                    return 0;
               } else {
                    return primes(x, i - 1);
               }
          } catch (Exception e) {
               return 1;
          }
     }
}

Upvotes: 0

user8492094
user8492094

Reputation: 1

import java.util.Scanner;
public class Primenumm {

static int limit,flag;

public static void main(String[] args) {
    // TODO Auto-generated method stub

    System.out.println("Enter the Number :-");
    Scanner s=new Scanner(System.in);
    limit=s.nextInt();
    int i=0,j=limit;
    printNum(i,j);
}

private static int  printNum(int i, int j) {
    // TODO Auto-generated method stub

    if(j==0){
        return 1;
    }
    if(i%j==0){
        return  0;
    }
    else{

        return  printNum(i,j-1);
    }
}

Upvotes: -1

dummy
dummy

Reputation: 1

public class PrintPrime {

    public static void main(String args[]) {
        for (int i = 2; i < 1000; i++) {
            primes(i, Math.ceil(Math.sqrt(i)));

        }
    }

    public static int primes(int x, double i) {
        if (i == 1)
            System.out.println(x);
        if (x % i == 0)
            return 0;
        else
            return primes(x, i - 1);
    }

}

Upvotes: -1

Christian Wilkie
Christian Wilkie

Reputation: 3823

At first glance, it appears that you're missing a return in your else statement:

public int primes(int x, int i)
{
    if(i==1)
        return 1;
    if(x%i==0)
        return 0;
    else
        return primes(x, i-1);
}

edit: Also, as said in rgettman's answer, there is a logical bug in the first conditional if(i==0). It should be if(i==1). After testing the code with the above edit here is my result:

List of primes under 100: 
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97

Upvotes: 1

krillgar
krillgar

Reputation: 12805

You just need a return inside your else.

else
    return primes(x, i-1);

The return there will return the results of the recursive calls, and they'll work their way up the stack. Though, this might lead to StackOverflowExceptions.

Upvotes: 0

rgettman
rgettman

Reputation: 178263

In this case:

else
    primes(x, i-1);

You aren't returning anything. However, the compiler must ensure that something is returned in all cases. Just return whatever the recursive method call returns:

else
    return primes(x, i-1);

Also, modify the first case's condition to i == 1 so it returns 1 on primes correctly.

Upvotes: 6

Related Questions