Sythe.Org Forums     Register     FAQ     Members List     Calendar     Mark Forums Read    
 
Sythe.Org Forums  
   Runescape Gold

Sythe.org — A Virtual Goods Trading Hub

Make real cash! buying and selling in-game items.

We have a no-scam policy.

You can make thousands playing your favourite games here at Sythe.org.

Just sign up an account and follow the rules!


Take me to

Runescape Markets

Other Game Markets

Support Center

Register an Account

Close
PrimeGenerator java help
Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 12-05-2010, 07:52 PM
Xeox's Avatar
Forum Addict
$25 USD Donor
 
Join Date: Jun 2010
Location: Watertown, CT
Posts: 346
Send a message via MSN to Xeox
Lightning PrimeGenerator java help

I am having a hard time figuring out an algorithm to calculate the prime numbers between 2 and x (where x is a number entered by the user and is exclusive).

I can't seem to figure out how to get the program to print the right numbers.

Example:
Enter a number: 20
Prime Numbers less than 20:

2
3
5
7
11
13
17
19

We are using loops this chapter (while/for/do), but I understand how I am going to loop it.

for (int i = 1 (unsure about the starting number); i < number (number entered ex: 20); i++)
{
//run the method that returns the right values
}

The for loop would be in the main class, but the method would be in a separate class called PrimeGenerator.

If you're confused by how I am asking the question you can just discard it, but if you know how to make an algorithm that can help me out here that would be great!
__________________
Reply With Quote
  #2  
Old 12-05-2010, 09:45 PM
Fate's Avatar
Apprentice
 
Join Date: Apr 2005
Posts: 774
Default Re: PrimeGenerator java help

You can make a boolean method that take's an int x as a parameter and loop it x amount of times while checking if it is prime. Such as:

Code:
boolean isPrime = true;
        for (int i = 2; i < number; i++) {
            if ((number != i) && (number % i == 0)) {
                isPrime = false;
                break;
            }
        }
Once it runs through the loop return isPrime.
Reply With Quote
Reply



Cheap RS Gold Store  Runescape Gold

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

All times are GMT +1. The time now is 04:51 PM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.6.1