# prime number « Number « Java Data Type Q&A

### 1. Prime number calculation fun    stackoverflow.com

We're having a bit of fun here at work. It all started with one of the guys setting up a Hackintosh and we were wondering whether it was faster than a ...

### 2. Problems with prime numbers    stackoverflow.com

I am trying to write a program to find the largest prime factor of a very large number, and have tried several methods with varying success. All of the ones I ...

### 3. Help with Java Program for Prime numbers    stackoverflow.com

I was wondering if you can help me with this program. I have been struggling with it for hours and have just trashed my code because the TA doesn't like how ...

### 4. Prime Numbers Code Help    stackoverflow.com

I am suppose to "write a Java program that reads a positive integer n from standard input, then prints out the first n prime number." It's divided into 3 parts. 1st: This ...

### 5. How to find all squares in the number (Java)?    stackoverflow.com

I seem to have a mental block, and cannot progress with the following problem. Basically, I want to find all possible squares in a given number, i.e. N = S*S*A, where ...

### 6. how do i output prime numbers in descending order using a stack?    stackoverflow.com

``````import java.util.Stack;

public class Primes{
public static void main(String[]args){
Stack<Integer> stack = new Stack<Integer>();

stack.push(null);
//number of primes to display
...``````

### 7. How do I ouput 50 prime numbers using a stack?    stackoverflow.com

I have tried and tried and still do not understand how to use a stack...i havent put my code in here because it is all wrong and I would just like ...

### 8. Whats the built in function that Finds next largest prime number in java?    stackoverflow.com

Does the Java API provide a function which computes the next largest prime number given an input x?

### 9. Is there a more efficient way to write the following Prime Number code?    stackoverflow.com

I have the following code which spits out prime numbers between 1 and N. A friend came up with this solution but I believe there is a more efficient way to ...

### 11. How to print 10 prime numbers per line and continue on the next line?    bytes.com

I tried what you had but I have never used the calculations function and I got several syntax errors. I tried to put it after my print line and also I ...

### 13. Java Prime Number Program    coderanch.com

As requested prior, please provide (ie: post here) what you have done so far (ie: post your code), explain what it is that you expect to happen and show what is occuring. That way the great folks here can help you understand where you are "going wrong" as well as documenting learnings for others.

### 14. Highest Number of Consecutive Primes    coderanch.com

Euler published the remarkable quadratic formula: n + n + 41 It turns out that the formula will produce 40 primes for the consecutive values n = 0 to 39. However, when n = 40, 402 + 40 + 41 = 40(40 + 1) + 41 is divisible by 41, and certainly when n = 41, 41 + 41 ...

### 15. truncatable prime numbers    coderanch.com

I have this question from euler The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3. Find the sum of the only eleven primes that are ...

### 16. Display the prime factors of a number using sets    coderanch.com

This program is supposed to do two things: 1) determine if a user input number is prime and if it is, the output will say as much (this works okay) 2) if it is not prime, it should display the number and its prime factors using a set(I'm lost) Here is the code and pseudocode I have for finding the prime ...

### 18. Identify all prime numbers between 100 and 200    coderanch.com

a prime is a number that yields an integer value when divided by any number other than itself (and 1). In practice the test does not have to be run for numbers greater than the square root of the number being tested rounded up to the next whole number. Also, only other prime numbers need to be taken into consideration (whether ...

### 19. prime numbers    coderanch.com

I need to write a program that finds the prime numbers between 1 and 50, but I'm not quite sure how to do it. I know that an even number is prime if it is two, and an odd is prime if it isn't divisible by any of the odd integers less than the square root of the number. Should I ...

### 20. prime number problem    coderanch.com

Follow a stepwise approach: 1> first decide the logic u want to use to detect if a number is prime or not 2> write a for loop for 0 to 500 and check if each number is prime or not 3> Start coding with whatever you know 4> Now post your doubts here. You will surely get a good response.

### 21. Prime Numbers    coderanch.com

Can you be more specific? In particular, do you need to generate prime numbers or do you need to determine if a given number is prime. One simple approach to the former is for i = 1 to n if i is prime print i (Note this is pseudocode so it will not compile in Java directly.) This relies on determining ...

### 22. Finding the Nth Prime Number    coderanch.com

Hello, I have to make a program that finds the nth prime number. Basically, the user types in a number, for example, 10 and the program outputs 29 (the 10th prime number). I have created my program and I think I must be on the right track since when I input 10 the program outputs 28. The actual output is always ...

### 23. prime number problem    coderanch.com

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 ...

### 24. Java Program for Prime numbers    coderanch.com

Hi All, I have a program here: /*Program to print prime numbers between 1 to n*/ while(i<=n-1) { if((n%i)==0) { break; } i++; } if(i==n) { System.out.println("The prime numbers between 1 and " +n+ " are:"); System.out.println(""+n); } n--; } } Please help me why this code does not lead me to the end results??? This is a program to find ...

//My aim is to find prime numbers from 1 to 100. Here i have done what i could think //of. //But i could not get the answer. Please let me know where i am wrong. public class prime { public static void main (String args[]) { int num ,i; for (num=1; num <=100 ; num++) { for ( i=2; i<=num; i++) ...

### 26. Returning if a number is prime. (Chapter on Objects and classes. OOP)    coderanch.com

Ok i search and im' sure there i couldn't find something like this. Design a class named MyInteger. The class contains: **An int data field named value taht stores the int value represented by thsi object. **a constructor that creates a MyInteger object for the specified int value **A get method that returns the int value. **Methods isEven(), isOdd(), and isPrime() ...

### 27. Finding n th prime number    coderanch.com

I am trying to write a code for a program that will find the n th prime number.As for example 7 is the 4th prime number,11 is the 5th. The method will take argument as n,and it will find the nth prime number.But I am facing some difficulty with the code.The code doesnt terminate by itself and doesnt return the desired ...

### 28. Finding prime numbers    coderanch.com

I am stuck and looking for some guidance with this problem. I have researched and found solutions to this problem online but none of them work for me. I need to implement a single method that is going to have an int x passed to it. The method will need to check all values between 2 and x and then print ...

### 29. Prime Number    coderanch.com

Are you trying to implement it in Java? From the few lines of code you have provided- I assume you are not familiar with Java. If its a homework question please DoYourOwnHomework , and you cannot expect anyone here to just give you the program for the same. The steps which I can think of- - Find the prime numbers within ...

### 30. Finding nth prime number    java-forums.org

public class Problem{ /** * @param args */ public int findAns(int n){ int count=0; int ret=0; while(count

### 31. Generate prime numbers within fiboncacci series?    java-forums.org

package javaapplication5; import java.lang.*; import java.util.*; public class Main { public static void main(String[] args) { int fib1,fib2,fib3,n=0,flag, int i; int a[]=new int; Scanner sc=new Scanner(System.in); n=sc.nextInt(); fib1=0; fib2=1; System.out.println(fib1+ " " +fib2); fib3=fib1+fib2; while(fib3

### 32. square root and prime numbers    java-forums.org

Hello It is the first time that I post on this site, so I do not know if it really good category. I try to write a small java program that determines if a number is prime or not, it looks like this: if (nombreEntr % 2 != 0) System.out.println(nombreEntr +" est un nombre premier"); // prime else if(nombreEntr % 2 ...

### 33. displaying prime number    java-forums.org

what is error in this code public class Exercise9_6 { public static void main(String[] args) { final int LIMIT = 120; int count = 0; StackOfIntegers stack = new StackOfIntegers(); // Repeatedly find prime numbers for (int number = 2; number < LIMIT; number++) if (isPrime(number)) { stack.push(number); count++; // Increase the prime number count } // Print ...

### 34. Prime Number finder    java-forums.org

Hello i am trying to write a program to find all the prime numbers in a given number range but i cant seem to do it. Here is what i have so far, i think im close but just cant get it right Java Code: public class MainClass { public static ArrayList interval( int m, int n ) { ArrayList result ...

### 36. Prime Numbers    forums.oracle.com

It's less than that -- you only need boolean values for the [Sieve_of_Eratosthenes|http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes]. I whipped up some code to compute the primes less than 2,000,000 and it only took a few millis of clock time to run. If space is an issue, you could sacrifice time and use a java.util.BitSet -- which packs 32 bits into an int.

### 37. problem making a prime number program    forums.oracle.com

silver, Yep the sieve of Erastothenes takes some comprehending... it's complicated... actually it's about as complicated an algorithm as you're ever likely to meet... except for maybe http://en.wikipedia.org/wiki/Sieve_of_Atkin (which is a lot faster, but is alas an order of magnitude more complicated). I suggest you have a go at Atkin... don't worry too much about understanding how the algorithm works (because ...

### 38. Finding Prime Numbers    forums.oracle.com

The program should allow the user to enter an upper limit (e.g., 100, 1,000, etc.) and the program should calculate and display all of the prime numbers from 1 to the upper limit. That's what I need to do. Now, I know how to get a number put in by the user and how to calculate if it's a prime number ...

### 39. Listing all prime factors of a given number    forums.oracle.com

I had 2 methods to write, and I have 2 hours before my class starts and i figure I can do another and maybe get some points for it. How would I go about listing all of the prime numbers of a given number. "listPrimeFactors(num) This function should list the prime factors for the given number." <------------------ that is the method ...

### 41. Prime number run problem    forums.oracle.com

This is my code for a tiny little program that finds the sum of all prime numbers in a certain range. It works for smaller numbers, i.e. when i make the end condition of the for loop i<10, i get a correct answer (17). I also get 77 when i make the end condition i<20. However, I'm trying to find the ...

### 42. Finding Prime Numbers:    forums.oracle.com

You are going to compute all primes less than N, and display the results step by step on the BlueJ terminal screen to show the working of your program. Upon completion, you are to summarize the computed results in an easy-to-read format. The program starts by asking for an integer value N from the user. Print out the initial matrix of ...

### 45. need help with a simple prime number output program    forums.oracle.com

package primetest; public class Main { public static void main(String args[]){ int num1,num2; int flag=0; //Checks to see if the number in question is prime or not for(num1=1;num1<=463;num1++) { flag=0; for(num2=1;num2

### 46. Prime Numbers    forums.oracle.com

You aren't printing the primes between 1 and 10,000, you're printing the first 10,000 primes. You start at one and loop 10,000 times, each time getting the next prime. So for the i=2, you get 2, for i=3 you get three, but for i=4 you get five, then 7, then 11, etc. See how the primes will end up higher than ...

### 47. prime number problem    forums.oracle.com

a private helper method to PrimeNumbers called checkAndResize(int n). The checkAndResize does nothing if n is in the sieve range (e.g., if n+1 is less than or equal to the length of sieve). However, if n is too large for the sieve, call initializeSieve with a parameter of 2*n. Modify the getNumberPrimeNumbers method to call checkAndResize with a parameter of n ...

### 49. prime numbers    forums.oracle.com

I think you need to sit back for a moment, and think about what you are doing. Do you understand what a method is? What are the methods defined in your class? What does each of the methods do? Format your code correctly. This will help you see what is going on in your class.

### 50. Help with prime number program    forums.oracle.com

} } Okay so how do I get the program then to tell me that the number is prime and without repeating my output statement everytime my counter changes the number. I tried it but for example, the number 7, I'll get an output 7 times that says "The number is not prime". I only want it to say that once. ...

### 51. Java help please.. prime numbers    forums.oracle.com

i am writting a program for java were a user has to input a number and the output has to say if the number is a prime or not.. i can figure out how to do all the code the only thing i'm missing is the formula for prime numbers.. could anyone possibly help me out... thanks

### 54. Help with printing prime numbers and their tenth    forums.oracle.com

You're implementing functions that don't seem to need state (simplified: things to store in instance variables) - you put in some arguments and get a result, so you would need getters and setters. I can't understand what printTenthOfPrime is supposed to do based on your code and comments: '10th of prime' makes no sense in English. You call one parameter 'primenumber' ...

### 57. Finding Prime Numbers    forums.oracle.com

Provide details please. For example, if the user inputs 23, it should output 2, 3, 5, 7, 11, 13, 17, 19, 23. What does it output instead? Also, you can help yourself by doing some basic debugging. Put in print statements so you can see what's happening at each step, which will help you figure out which of your assumptions about ...

### 59. help with sexy prime number code    forums.oracle.com

import java.util.Scanner; public class xxxx_Sieve { //An array of 50001 boolean values that will represent numbers that are prime. private boolean primes[] = new boolean ; private int upper; private int lower; //This is the constructor. It should initialize elements 0 and 1 to false and the rest of the array's elements to true. It should als initialize lower to 1 ...

### 60. Prime number    forums.oracle.com

while (primeInx < no) { boolean prime = true; for (int j = 2; j < i; j++) { if (i == i / j * j) { prime = false; } } if (prime) { primes[primeInx++] = i; } i++; } return primes; } public static void main(String[] args) { new TimeExec(new Runnable() { public void run() { int[] primes ...

### 61. checking the command line argument value(number) is prime number or not ?    forums.oracle.com

class number { public static void main(String args[]) { if (args.length == 1) { try { int a = Integer.parseInt(args); if((a%2)==0) { System.out.println("given numbers is even"); } else { System.out.println("given numbers is odd"); } int num = a; int i; for (i=2; i < num ;i++ ) { int n = num%i; if (n==0){ System.out.println("not Prime!"); break; } } if(i == ...

### 63. Returning Prime Numbers    forums.oracle.com

The function getPrimes() is so declared that it needs to return a String but in a question of generating all prime numbers, i dont know what use will the String returning function be put to. If you intend to return a string containing all prime numbers and display the same string as output, then find each prime number and convert it ...

### 64. Prime Numbers!!    forums.oracle.com

Thanks! I did exactly what you said, but still doesn't work. maybe you are right im incrementing the counter quite often and i should have used while loop but i just get confused with the while loop coz suppose if you haven't written the code correctly then the only thing it does is crashes the program every time you run it. ...

### 65. Need help with finding prime numbers    forums.oracle.com

This does not describe the algorithm at all, and it does use Java. Both are the opposite of what I suggested. 2. I use Netbeans 6.1, and the second for statement is flagged as an error. It says incompatible types > found: int > required: boolean for (divisor = 3; divisor = incrementer; divisor = divisor +1)

### 69. Prime Number Generator    forums.oracle.com

Not surprising. You assign 3 to a variable, half that i.e. 1 to another variable, then loop while the second variable is greater than 1, which it isn't. If that's not what the code is supposed to do, fix it so it does it. Or benefit by the work of Eratosthenes ...

### 70. Fastest prime number finder    forums.oracle.com

Er.... waitaminnit... You start base at 3, then you add 1, check for %2=0, etc.... Why not just always add 2 and forget the %2=0 altogether? You never need to check the evens, so if you start at 3 and always add 2--3, 5, 7, 9, etc.--you never need to check %2.

### 71. listing prime numbers 1-100    forums.oracle.com

I'm making a program which outputs all prime numbers between one and a hundred, without just using System.out.println ("...") to show each one. I need to use for and loop until it hits 97, which is the last prime number before one hundred.. Does anybody know a formula I could run an int through to display all the primes, or any ...

### 74. Prime Number Program    forums.oracle.com

Make a list of numbers from two to your maximum. A resizeable list would probably be the easiest to work with. Start looping through your list. For each number you come across, remove all the numbers greater than your number which are divisible by that number. In the end, you will have a list of primes. ~Cheers

### 75. Finding if a number is prime    forums.oracle.com

**** buggers all over the place. I didn't unit test it for squares of primes...just typed something up and threw it out there...sorry for the lack of testing and reflection on all code I post.....good catch, but i was refering to the mistake as not being that big of a deal. I wasn't infering that it worked or anything.

### 76. Creating a Program for Identifying Prime Numbers    forums.oracle.com

1. Create a main method, and a method called ArrayList< Integer > sieve( int n ), which returns a list with all the prime numbers less than n. The main method calls sieve( 10,000 ), and prints out the results. 2. Inside of sieve(), do the following: a. Call method createTrueArray( n ) to create a boolean array of size n. ...

### 77. Prime Number Programming    forums.oracle.com

Compute all primes less than N, and display the results step by step on the BlueJ terminal screen to show the working of your program. Upon completion, you are to summarize the computed results in an easy-to-read format. The program starts by asking for an integer value N from the user. Print out the initial matrix of numbers from 2 to ...

### 78. Find Prime number    forums.oracle.com

public static boolean isPrime(long n) { boolean prime = true; for (long i = 3; i <= Math.sqrt(n); i += 2) if (n % i == 0) { prime = false; break; } if (( n%2 !=0 && prime && n > 2) || n == 2) { return true; } else { return false; } }

### 80. Program to find the prime(s) of 2 numbers.    forums.oracle.com

What I am trying to do here is have the var(t) start at 2, and it will see if it devides into var(x) evenly, if it does, it will move on to the next X number, "x++". But if it does not devide evenly in, it prints out X. I do not see where I went wrong, yet Im still new ...

### 82. Prime Number Program    forums.oracle.com

Hello Fellow Programmers Im having quite alot of trouble trying to solve this problem, i was hoping you could help me. Anyway, Im trying to make a program that when a user enters a positive integer, the program will find out if it's prime or not, im really stuck...any help is awesome!! Whoops, i forgot to mention that i am trying ...

### 83. Finding prime numbers    forums.oracle.com

Hello, I am currently taking an intro to java class and i was looking for help on the following problem. I need to find all the prime numbers before N than display them along with how many prime numbers there are. I cant put a counter in the loop since that will be equal to i and anything i put in ...

### 84. Help Needed, Prime Numbers...    forums.oracle.com

Hey everyone, after researching everything this is what I came up with... I'm actually a bit dissapointed because I think its rather sloppy but hey its functional... of course I completly welcome suggestions to make it for efficent, clean, and simple. And yes im not using functions. Thanks, Xavya import java.util.Scanner; class numprime2 { public static void main(String [] args) { ...

### 85. Prime Numbers Help    forums.oracle.com

we have an assignment for my programming class. We get an integer input from the user, and using a loop system, we are to figure out if the number is prime or not, and then print to the user whether the number they just entered is prime or not. Here is what i have so far: int value=1, n=0; Scanner scan ...