I am trying to list of prime number from n to m but my program give only one number
import java.io.PrintStream;
import java.util.Scanner;
public class Check05B
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
PrintStream output = System.out;
output.print("Enter a number to test: ");
the prime numbers from 1 to 2500 can be obtained as follows. From a list of the numbers of 1 to 2500,cross out al multiples of 2 (but not 2 itself). Then, find the next number (n, say) that is not crossed out and cross out all multiples of n (but not including n).
Repeat this last step provided that n has not exceeded 50 (the square root of 2500). The numbers remaining in the list (except 1) are prime. Write a program which uses this method to print all primes from 1 to 2500. Store your output in a file called primes.out.
public class printprimes2{ public static void main(String[] args){ for( int i = 1 ; i <=199 ; i++ ) //iterate 1 - 199; 2 is prime { for ( int j = 2 ; j < i ; j++ ) //iterate 2 - potential composite EXCLUSIVELY; every number can be divided by one and itself
[Code] ....
It doesn't print only prime numbers but all numbers that range from 0 to 199. What do you think I am doing wrong?
I am writing a program that checks if a number is prime or not. Code below.
for(int i = 2; i < P; i++){ if (P % i == 0) { System.out.println(P + " Can also be divided: " + i); return; } } System.out.println("Prime number.");
It works but, if a number is not a prime. I need to print out all the numbers that it can be divided with. For example if a number would be 8: it can also be divided with 1, 2, 4, 8.
The assignment is to make a program that prints the number of prime numbers in a range. This is what i have so far. The output is a list of 2s. I created the for loop to cycle through the range of 17-53 and nested a while loop within to test each incident of the for loop to check for divisors starting with 2 until the modulus result is 0 resulting in a false for being a prime number. Then the loop should increment to the next i value. The last part is an if statement that i had intended to add counters to the k variable that would keep track of the number of prime numbers.
boolean isPrime = true; int j = 2; int k = 1; for (int i = 17; i <= 53; i++){ { while (i % j == 0){ isPrime = false;
I have been working on a Project Euler problem which is to find the 10001st prime number. I made the project in java and it gave me the correct answer (104743) . I notice that it had taken 17 seconds to find the answer. how to improve the efficiency of my Java program - at the moment it is bruteforce.
static boolean isPrime; static int primeCount; static int forI = 1; static int latestPrime; public static void main(String[] args){ long startTime = System.currentTimeMillis();
One of our assignments this week was to make a program that when given a number it will return the biggest prime factor of that number. So for example if the program is given the number 15, the output is 3.
144 gives you 3 also. 17 gives you 17 21 gives you 7.
Anyways, after some time trying out various things and combining stuff i googled with my textbook I somehow stumbled upon a code that works. But I cannot for the life of me understand how it works. I think it has something to do with the inner loop and the outer loop. But I feel like i cant go on without understanding how it works.
Here is the code:
public class BiggestFactor { public static void main( String[] args ) { int N; N = StdIn.readInt(); int n = N;
Any better way to write a program that takes a user number input and the program determines whether or not the number is prime or not. It was suppose to be a number between 0 and 8,000,000.
import java.util.Scanner; public class prime1 { public static void main(String args [])
I am new to java programming and using bluej for programming and i have tried this question what i have have given in title ... how to find out the errors in the following program:-
class Primedig { public void getinput(int n) { int ctr=0; while(n!=0) { int r=n%10; for(int i=1;i<=r;i++)
[code] .....
output:-
For example input is 243 the output comes only 3 not 2& 3 both.
I want to find the prime palindrome numbers less that a given number by my program. Here is my code, I am trying to find the method to solve the errors when I compile it. It said variable a might not have been initialized in line 41,62,86.
import java.util.Scanner; public class Lab5{ public static void main (String[] args){ System.out.println("Please input a number"); Scanner Input=new Scanner(System.in); int num = Input.nextInt();
Program is to list all prime numbers between two entered numbers.
import java.util.Scanner; public class question6 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter lower int:"); int x = input.nextInt();
I have been struggling with this program for weeks. This program is supposed to take a user's inputted odd number and then print out all prime numbers lower than that number.
public class PrimeNumber { Scanner scan = new Scanner(System.in); int userNum; String neg;
So I am currently writing my first assignment and have run into problems with my coding. The task was to have someone enter a 5 digit number and in return, I list each number on their respective lines. We also must create an error if a number other than 5 digits was entered. My problem is that when they enter a 1 or 2,3,4,6,7,8 digit number.. the error message occurs along with the rest of the messages (listing the numbers, etc). I want the program to end (or even re-ask to enter the numbers) if they incorrectly enter the data.
I am trying to count the number of occurrences of a string in an array list. I used the following code:
int count = Collections.frequency(strings, search);
strings is the name of the array list and search is the string that I am trying to count the number of occurrences of. My program compiles correctly, but when I enter the string to search for, I get the following error: "Exception in thread "main" java.lang.NullPointerException at java.util.Collections.frquency(Collections.java:37 18)
Why am I getting this error message and how do I fix it?
Write a method maxOccurrences that accepts a list of integers as a parameter and returns the number of times the most frequently occurring integer (the “mode”) occurs in the list. Solve this problem using a map as auxiliary storage.
//checks wether an int is prime or not public static void main(String[] args) { int n = 17; boolean prime = true; if (!(n==2)) { for (int i = 2; i * i <= n; i++) { if (n % i == 0);
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println(" Enter the maximum number "); int maxNumber = scanner.nextInt(); int[]numberList = createList (maxNumber);
[code]....
I have a problem with the output. It only get as far as marking the multiples of 2. It does not mark the multiples of 3 and so on.Example output: If the range is from 2 to 15 the output is :2, 3, 0, 5, 0, 7, 0, 9, 0, 11, 0, 13, 0, 15 note: 0 means its marked.As you can see it only marks multiples of 2 but it does not mark multiples of 3 above. I think the problem lies in the for loop inside my main method, because the index does not increment.
The challenge is to weed out all the prime numbers without using any kind of division (%, /). My code doesn't weed out certain numbers, such as many multiples of 5, the number 49, etc, and I am not sure why. Here is my code.
My logic for the for loops was this: Starting with the upper numbers of the ArrayList, find every number that is a multiple of that number and remove it from the ArrayList. Every time you find a multiple, increase the variable multiply, so the program knows what the next multiple to look for.
// program doesn't work yet. import java.util.ArrayList; // import java.util.ListIterator;
public class Sieve2 { public static void main(String[] args) { int upperLimit = 55; ArrayList<Integer> primes = new ArrayList<Integer>();
Program to pull prime numbers between two entered values,
import java.util.Scanner; public class question6 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter lower int:");
I have an endpoint that will determine if a passed in integer is prime or not. Should the request to this endpoint be GET or POST? Right now, I'm thinking it should be GET since it doesn't do anything to any resource.
The game doesn't seem to be working but you can win by loading a winning saved game.
Hint: Remember that all numbers have a unique set of Prime Factors.
I have been struggling to solve this. The code is not in error. I am trying to load a winning file but unable to solve.
Here is the java code:
import java.math.BigInteger; import java.util.ArrayList; import java.util.HashSet; import java.util.Random; import java.util.Scanner; import java.util.Set; public class Main { static final int GAME_SIZE = 40; //Disk sizes go from 0->39 // Produce a list of the first N prime numbers
[Code] ....
Results:
C:UsersSal_2>java Main Welcome to Towers of Toast!!! Type 'new' to start a new random puzzle Type 'load' to load a saved puzzle new [0, 2, 3, 4, 5, 6, 8, 10, 14, 19, 20, 25, 26, 28, 35, 38, 39] [7, 9, 11, 13, 15, 21, 22, 23, 27, 31, 32] [1, 12, 16, 17, 18, 24, 29, 30, 33, 34, 36, 37] | | XX | | XXX | | XXXX | | XXXXX | | XXXXXX | X XXXXXXXX XXXXXXX XXXXXXXXXXXX XXXXXXXXXX XXXXXXXXX XXXXXXXXXXXXXXXX XXXXXXXXXXXXXX XXXXXXXXXXX XXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXX XXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
The game is broken! We saved your game, though. Here are your save game numbers:
C:UsersSal_2>java Main Welcome to Towers of Toast!!! Type 'new' to start a new random puzzle Type 'load' to load a saved puzzle load Enter save number for pole 1: 3 Enter save number for pole 2: 1 Enter save number for pole 3: 11 Exception in thread "main" java.lang.RuntimeException: Not all disks accounted f or at Main.main(Main.java:100)