Ok, I think I may have misguided you or at least added to your confusion.
I'LL review you code. You have two loops.
One counting up to n which is 100 in this case.
Another which test for the prime property. This loop counts down for each i value it receives
The inner loop:
for(x =i; x>=1; x--)
{
if(i % x == 0)
{
ctr = ctr + 1;
}
}
What you are doing here is: for each number you receive (i), you test for prime quality.
You do this by checking every value from i to 1 inclusive to see if it evenly divides i. This is ok but, if your limit for n was large, this method will be very slow. But I guess it is ok for 100.
What Sinipull was saying here:
Quote
Also, there's no need to check any primes above the i/2 line. For example 100 can not evenly divide by any number over 50, because it's certain, that the answer is less than 2.
is basically this:
Think of the number 10. There is no number greater than 5 that can divide 10 Evenly.
Think of the number 18. There is no number greater than 9 that can divide 18 Evenly.
Then you only need to check for divisors half the size of i and go down; cutting your prime test by half for each value i you receive.
Which would make you loop look more like this:
for(x = (i / 2) + 1 ; x>=1; x--)
{
if(i % x == 0)
{
ctr = ctr + 1; /* check for 1 in the outer loop; two or more would be not prime */
}
}
Play around with a few numbers to get the gist.
When i said:
Quote
If my math is correct you only need to check up to square_root(n) where n is the 100 in this case.
I was speaking generally about solving the prime number problem, in terms of making you algorithm more efficient.
In your case; if you were testing 64 for the prime quality and you started from square_root(n) and decremented like you did in your code
you may end up with incorrect results.
i.e square_root(64) = 8 ---> You check 8 7 6 5 4 3 2 1. But numbers like 16 and 32 evenly divide 64.
A better approach for solving the prime problem is using
Sieve of Eratosthenes - Wikipedia, the free encyclopedia.
I have a copy so ill save you some of the trouble.
//isPrime.c
#include <stdio.h>
#include <math.h>
#define N 100 /* limited only by memory size */
#define SQRTN ((long) sqrt(N))
int generatePrimes(int prime[])
{
/* ------------------------------------------------------------------------- *
* This is the classic prime sieve of Eratosthenes - a simple algorithm *
* that finds all the prime numbers between 2 and the constant 'N'. *
* n is prime <=> prime[n] == 1 *
* Name : sieve.c (Sieve of Eratosthenes) *
* Author : Steve Park & Dave Geyer *
* Language : ANSI C *
* Latest Revision : 9-15-96 *
* Adapted by : *
* ------------------------------------------------------------------------- */
int temp;
long i; /* index of possible primes */
long s; /* step index */
prime[0] = 0; /* initialize the sieve */
prime[1] = 0;
for (i = 2; i <= N; i++)
prime[i] = 1;
for (i = 2; i <= SQRTN; i++) /* search all possibilities */
if (prime[i]) /* if n is prime, */
for (s = 2; s <= (N / i); s++) /* 2n, 3n, 4n ... can't be prime */
prime[s * i] = 0;
}
int main()
{
int prime[N + 1];
int i;
generatePrimes(prime);
for(i = 0; i <= N; i++)
if(prime[i]) printf(" % d",i);
printf("\n");
return 0;
}
I'm hoping this helps.
---------- Post added at 01:41 PM ---------- Previous post was at 01:37 PM ----------
Edit: You can translate that C to java without to much taught.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused: