hey i am meant to write a method to display a number of prime numbers specified by the user.
here's my code and i know its prob all wrong and there are much easier ways to do it but i just wanna know what to do to make it compile and i'll work from there.
So far the debugger says "missing return statement"
Any help would be appreciated, thanks.

static boolean loop(int n)
    {
        int[]a = new int[n];
        a[0] = 2;
        a[1] = 3;

        System.out.print(a[0]);
        System.out.print(" ");
        System.out.print(a[1]);
        System.out.print(" ");

        for(int i = 2; i < Math.sqrt(n); i ++)
        {
            if(a[i] % i == 0)
            {
                System.out.print(a[i]);
                System.out.print(" ");
            }
        }
        System.out.println();
    }

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Well prime nos re those which are divisible by 1 or by themsleves only.


Well ur code should be something like this

assuming the range to be like 100 .

ArrayList<Integer> a = new ArrayList<Integer> ();
int range =100 ;
a.add(2);
a.add(3);
for(int i= 4 ; i<range ; i++)
{
       for( int j = 2 ;j <i ; j++)
       {
               if( i %j ! = 0 )
                 a.add(i);
        }
}         
  
System.out.println( " the list of prime nos are " + a);

i think this should do , well i would be glad if you could run the code

assuming the range to be like 100 .

ArrayList<Integer> a = new ArrayList<Integer> ();
int range =100 ;
a.add(2);
a.add(3);
for(int i= 4 ; i<range ; i++)
{
       for( int j = 2 ;j <i ; j++)
       {
               if( i %j ! = 0 )
                 a.add(i);
        }
}         

System.out.println( " the list of prime nos are " + a);

For better run time requirement I'm changing it a bit.

ArrayList<Integer> a = new ArrayList<Integer> ();
int range =100 ;
a.add(2);
a.add(3);
int flag=0;
for(int i= 5 ; i<range ; i+=2)
{
       for( int j = 2 ;j <Math.sqrt(i) ; j++)
       {
               if(( i %j ) == 0 )
               {
                     flag=1;
                     break;
                }
        }
         if(!flag)
         a.add(i);
}         

System.out.println( " the list of prime nos are " + a);

See, all we know that prime numbers are odd except 2. So why should we go for checking even numbers?
That's why I write

for( int j = 2 ;j <Math.sqrt(i) ; j++)

Think this can help you.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Well , i didnt think of run time at the first place , i just thought of logic . but instead of checking for Math.sqrt() u can actually check till "i/2" in the "j" iteration loop , i guess that will save more time . (do that in the original code i wrote) . i would be glad if you could check and compare the time of execution .

Hi all
What if we counts the divisors?If more or less as 2 then Not prime else if exactly 2 then prime...(only 1 and itself)
Like in pascal there is a function Mod always return the reminder(s), and if the function returns 0 the the counter variable increasing by 1.In the pascal snippets I wrote a little program that writes the prime and not prime numbers from 0 to 100.The method is that I count the divisors' numbers....
Sorry I'm just knowing the pascal a bit...:)

Hi all
What if we counts the divisors?If more or less as 2 then Not prime else if exactly 2 then prime...(only 1 and itself)
Like in pascal there is a function Mod always return the reminder(s), and if the function returns 0 the the counter variable increasing by 1.In the pascal snippets I wrote a little program that writes the prime and not prime numbers from 0 to 100.The method is that I count the divisors' numbers....
Sorry I'm just knowing the pascal a bit...:)

Well thats the most lousy way of programming . why take a 5 mile road to reach ur neighborhood .

Well thats the most lousy way of programming . why take a 5 mile road to reach ur neighborhood .

Thx for classify... :yawn:

Well , i didnt think of run time at the first place , i just thought of logic . but instead of checking for Math.sqrt() u can actually check till "i/2" in the "j" iteration loop , i guess that will save more time . (do that in the original code i wrote) . i would be glad if you could check and compare the time of execution .

Well for a sufficiently large value to check, you can place the Math.sqrt() outside for loop like-

int loop=Math.sqrt(i);
for( int j = 2 ;j <loop ; j++)
{
.
.
.
}

In this case we require only one time to calculate square root and it will definitely reduce the execution time.

But Rahul, your program will give a wrong result. Consider i=9.
Then for j values 2, 4, 5, 6, 7 & 8 (i%j) will give a non-zero value. So 9 will be added to the array 6 times, but 9 is a composite integer.

Well u have understood me wrongly......
at the frist place i have checked the program and it works fine .
for 9 it will check 9 % 3 which is very much divisible . thus it will come out the loop .and not add into the array

Well u have understood me wrongly......
at the frist place i have checked the program and it works fine .
for 9 it will check 9 % 3 which is very much divisible . thus it will come out the loop .and not add into the array

How do the control come out of the loop if there is no break statement?

Check it twice, or run it.

by come out of loop i meant the if condition wont execute

if( i %j ! = 0 )
a.add(i);

if none of the nos are divisible other than itself and one its a prime no
i.e if the "if condition" satisfies (please re look at my code again) then only the number is added in array.or else the next iteration is done .

This article has been dead for over six months. Start a new discussion instead.