//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);
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: ");
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 [])
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.
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();
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.
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)
Suppose i have a string which has certain file names, S = "a.png, b.gif, c.xlsx, d.docx, e.xlsx, f.gif";I need to check if the string has more than one .xlsx file names,
I just can't find a way to check if user puts any input or not? The line is Employee Name and I need to validate that he puts something there.
import java.util.Scanner; public class JavaMartInventorySystem { public static void main(String[] args) { String empName; Scanner keyboard = new Scanner(System.in);
I have a programming assignment in which I have to make a program which loads a crossword from a Properties file and then, when the user press a button, the program checks if the letters the user has typed in the interface are the same as in the correct matrix. If all the letters from a word are correct, the program must paint every letter of the word green.
I'm using two matrix, one that is called "mundo" (I 'm from Latin America) , which has the correct letter for each box. The other one is a JtextField matrix which ='ve created using a Gridllayout. I called this one crucigrama (crossword in Spanish)
This is the code I wrote to validate: So you can understand, here is a translation:
numero = number fila = row column = columna bien - a boolean I used to check if the letter I've checked before are the same as the correct ones .darLetra() - returns a string with the correct letter
which method i can use so that the program checks if the input value is a digit or else outputs a message saying that `the value entered is not a number.