• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

prime number problem

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, hope you can help me out here. I need to generate all the prime number factors for the number 999999 (3, 7, 11, 13, 37). Here is my algorithm:

step n k comment
------------------------------------------------
0 999999 2 not divisible by 2, k = k + 1
1 999999 3 n is divisible by 3, n = n / 3, print 3
2 333333 3 n is divisible by 3, n = n / 3, print 3
3 111111 3 n is divisible by 3, n = n / 3, print 3
4 37037 3 not divisible by 3, k = k + 1
5 37037 4 not divisible by 4, k = k + 1
6 37037 5 not divisible by 5, k = k + 1
7 37037 6 not divisible by 6, k = k + 1
8 37037 7 n is divisible by 7, n = n / 7, print 7
9 5291 7 not divisible by 7, k = k + 1
10 5291 8 not divisible by 8, k = k + 1
11 5291 9 not divisible by 9, k = k + 1
12 5291 10 not divisible by 10, k = k + 1
13 5291 11 n is divisible by 11, n = n / 11, print 11
14 481 11 not divisible by 11, k = k + 1
15 481 12 not divisible by 12, k = k + 1
16 481 13 n is divisible by 13, n = n / 13, print 13
17 37 13 STOP since 13^2 > 37 print 37


I had no problem generating all the factors for 999999 - but I can't seem to get my isPrime method to work, included below is a copy of my code. It won't compile because apparently I have erros on lines 18 and 33, 18 is the beginning of the isPrime method, and 33 apparently I need a ; but I can't figure out why I could possibly need one there. Hope you all can help me out. Thanks in advance

public class Primenum
{
public static void main (String[] args)
{

int n=999999;
System.out.println(n+ "'s factors are ");
for (int i = 1; i <= n / 2; i++)
{
if (n % i == 0 && isPrime(i))
{
System.out.print(i+ " ");
}
}



public isPrime()
{

int n=number;
int j;

for (j=2; j<i; j++)
if (j%i != 0)
System.out.print(" ");
j++;

else
j++;

}
}

}
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Robert, there are a few things going on with this code. I've tried to highlight the errors I see, but there may be more.




Garrett
[ March 06, 2006: Message edited by: Garrett Rowe ]
 
Robert Fuhrman
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Garrett, thanks for your input. Am I correct in what I am attempting to do though? In the main method I am trying to get all the factors of 999999 which I think is correct...my isPrime method is probably totally screwed up lol - what I am trying to do is take the factor of 999999 that I found in the main method and test it to see if its prime - I thought I could accomplish this by that for statement i have in there. I used i in the statement because i is the factor of n , then using that loop to test for its factors. Once I do test for it's factors and find them, do I need to make another method for output?Is this the right way to approach this problem or am I way off base?

thanks for your help
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The way your code is set up will work. For reusability though, I would consider making three seperate methods.
 
Robert Fuhrman
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Garrett, I can't seem to get the factor from my main method to test in the isPrime method...any ideas?
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What does your main() method and isPrime() method look like. What problems are you having?
 
reply
    Bookmark Topic Watch Topic
  • New Topic