Register for your free account! | Forgot your password?

Go Back   elitepvpers Coders Den Java
You last visited: Today at 01:38

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



Prime Numbers

Discussion on Prime Numbers within the Java forum part of the Coders Den category.

Reply
 
Old   #1
 
kiko298's Avatar
 
elite*gold: 0
Join Date: Apr 2010
Posts: 115
Received Thanks: 46
Prime Numbers

Hi, im new to java and my code isn't working. It was supose to give me "all" prime numbers but it doesnt :/

Code:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class NumerosPrimos {

	static int numeroatestar = 2;
	static int numeroadividir = 2;
	static int resultado;
	
	
	public static void main(String[] args)
	{
		while(true)
		{
			try {
				Thread.sleep(500);
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			//Mostra o numero a testar
			System.out.println("A testar numero: " + numeroatestar);
			//Loop, vai dividindo ate que o numero pelo qual se esta a dividir iguale o que estamos a dividir 
			while(numeroadividir < numeroatestar)
			{
				if(numeroatestar % numeroadividir == 0)
				{
					//se for divisivel o que fazer
					numeroadividir++;
				}
				else
				{
					System.out.println(numeroatestar + " e um numero primo!");
					
			 
					numeroadividir++;
					
				}
			}
			
			numeroatestar++;
			numeroadividir = 2;
		}
	}
}
kiko298 is offline  
Old   #2
 
Mikesch01's Avatar
 
elite*gold: 203
Join Date: Sep 2007
Posts: 732
Received Thanks: 190
You have to look at the usage syntax for static variables in java:

You declaration was right but for usage you need the class name to access the right static variable. Example: MyClass.myStaticVariable
Mikesch01 is offline  
Old   #3
Moderator


 
elite*gold: 558
Join Date: Feb 2010
Posts: 6,548
Received Thanks: 1,418
Code:
public class NumerosPrimos
{
    public static void main(String[] args)
    {
        int numeroatestar = 2;
        int numeroadividir = 2;
        boolean found;
    
        while (true)
        {
            try {
                Thread.sleep(500);
            } catch (InterruptedException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            
            //Mostra o numero a testar
            System.out.println("A testar numero: " + numeroatestar);
            //Loop, vai dividindo ate que o numero pelo qual se esta a dividir iguale o que estamos a dividir 
            found = false;
            while (numeroadividir < numeroatestar)
            {
                if (numeroatestar % numeroadividir == 0)
                {
                    found = true;
                    break;
                }
                
                //se for divisivel o que fazer
                numeroadividir++;
            }

            if(!found) // found == false
            {
                System.out.println(numeroatestar + " e um numero primo!");
            }
            
            numeroatestar++;
            numeroadividir = 2;
        }
    }
}
It is a logical error, but I can not explain it in English. I have added the boolean 'found'.

Your Code:
4 % 3 == 1 -> Prime Numer
But 4 % 2 == 0 -> Not prime Number

Quote:
Originally Posted by Mikesch01 View Post
You have to look at the usage syntax for static variables in java:

You declaration was right but for usage you need the class name to access the right static variable. Example: MyClass.myStaticVariable
No, all right!

Das Problem im Code ist ein Logikfehler und kein Syntaxfehler. Man muss bei statischen Variablen nicht nicht Klasse dazu schreiben, wenn man aus der selben Klasse darauf zugreift.
ComputerBaer is offline  
Reply


Similar Threads Similar Threads
[c++] array with prime numbers
11/25/2012 - C/C++ - 2 Replies
Hi, i'd need some help with a function... well, i need to write a function with an array parameter and an int parameter. that array has to be filled with first 10 prime numbers that are exact or higher than the int parameter... and then i need an average value of those 10 prime numbers... The problem is im not really sure how i should do the part to fill the array with prime numbers that are higher than that int?? So any code samples that does that would be really nice... (the average...
best numbers
07/25/2011 - DarkOrbit - 1 Replies
what is the best radius for circling a bk for the circle tool, the increase and decrease things dnt work, i am a goli with all 15 speed gens, but i use battle side with no speed only prom on bk's?
charname numbers or guild numbers will take block?
02/21/2010 - Silkroad Online - 6 Replies
charname numbers or guild numbers will take block? some friends said that to me is it right?



All times are GMT +2. The time now is 01:38.


Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2026 elitepvpers All Rights Reserved.