Prime Numbers - Find N From List That Is Not Crossed And Cross Out All Its Multiples

Jun 20, 2014

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.

View Replies


ADVERTISEMENT

List All Prime Numbers Between Two Entered Numbers

Oct 17, 2014

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();

[Code] .....

View Replies View Related

Sieve Code To Find Prime Numbers

Jan 16, 2015

I am doing a sieve code to find prime numbers.

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.

View Replies View Related

Unable To Find Prime Palindrome Numbers Less That A Number By Program

Oct 10, 2014

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();

[Code]...

View Replies View Related

Long Type Output File - Print Prime Numbers Between Given Range Of Numbers

Aug 22, 2014

I tried to create file and write the output of my program in it in java when i use WriteLong then the file does not contain long value, how I create this file my program is to print prime numbers between 500000 to 10000000

public class primenumber {
public static void main(String[] args) {
long start = 5000000;
long end = 10000000;
System.out.println("List of prime numbers between " + start + " and " + end);
for (long i = start; i <= end; i++) {
if (isPrime(i)) {
System.out.println(i);

[Code] ....

View Replies View Related

List Prime Number From N To M

Oct 15, 2014

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: ");

[Code] ....

View Replies View Related

Printing Number Of Prime Numbers In A Range?

Feb 13, 2015

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;

[Code] .....

View Replies View Related

Weed Out All Prime Numbers Without Using Any Kind Of Division?

Dec 15, 2014

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>();

[Code] .....

And this is the output:

1 2 3 5 7 11 13 17 19 23 25 29 31 35 37 41 43 47 49 53 55

View Replies View Related

Java Program - Find Next Highest Or Lowest Prime Number

May 2, 2014

Java program takes positive int from user and determine if prime. Next the program finds next highest or next lowest prime number.

import java.io.*;
import java.util.*;
public class pa2take2 {
public static void main (String[]args){
int menus; //menu selection

[Code] ......

View Replies View Related

Program To Pull Prime Numbers Between Two Entered Values

Oct 18, 2014

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:");

[Code] ....

View Replies View Related

Loading Winning Saved File - All Numbers Have Unique Set Of Prime Factors

Nov 7, 2014

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:

146209709250387100944095470
52067710192022655041
21882855117530023387473

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)

C:UsersSal_2>

View Replies View Related

Print 2 Lists - 20 Random Numbers And Another List Without Duplicate Numbers

Feb 1, 2015

I'm trying to make a program that generates 20 random integers between 1 and 20 and then prints the list of random numbers to the screen. After that, I want to print a different list to screen with the same numbers from the first list only skipping any number that has been already printed to the screen. So two lists are printed to the screen. The first one has 20 random numbers. The second one has those same 20 numbers but only prints the numbers in the first list that aren't duplicated. So if m

y list of 20 random integers contains three 2s and two 14s, only one 14 and one 2 is printed to the second list. Currently, my code generates 20 numbers from 1 to 20 and stores those numbers in an array but I don't know how to print solve the second part of my problem. I don't know how to print the s different list only without duplicate numbers. As a result, my output is nothing because it doesn't print any number from the first list as oppose to skipping only duplicate one.

public void randomNum(){
System.out.println("Twenty random integers: ");
int max = 20; // max value for range
int min = 1; // min value for range
Random rand = new Random();
int[] all = new int[20];

[Code] ....

View Replies View Related

Program That Takes User Odd Number And Print Prime Numbers Lower Than User Input

Nov 4, 2014

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;

public void getUserNum()

[code]...

View Replies View Related

Multiples While Loop

Apr 7, 2015

I'm trying to get this program to work but it keeps giving me all multiples, including ones over 1000 when I want it to not go over 1000. What am I doing wrong?

* A program that will prompt the user to enter a number and
* then print out all the multiples of that number that are less than 1000.
*
*/
import java.io.*;
import java.util.Scanner;
import java.text.*;
 
[code]....

View Replies View Related

Mod Function With Multiples?

Jan 29, 2014

where I have to write a Java program that asks for user input regarding a book and provides information based on that input like the number of pages required, number of words on the final page and if the final page is more than half full, etc. We are told that each page should have 250 words maximum to use for our calculations. The problem I am having here is that when the total number of words is a multiple of 250 the output tells me that there are 0 words on the last page when in fact there are 250. I've tried messing around with mod functions in this format ((x+a)%250)+y which is what the professor told us to use instead of creating an if-statement for this case but I haven't been able to crack it. I don't think I fully understand how the mod function cycle could work for this case.

import javax.swing.JOptionPane;
public class CorreiaFranciscaA1Q1 {
public static void main(String[] args) {
String ManuscriptTitle =
JOptionPane.showInputDialog ("Please enter a book title.");
String AuthorName =
JOptionPane.showInputDialog ("Please enter the author name.");
String TotalWords =
JOptionPane.showInputDialog ("Please enter the number of words.");

[code]....

Above if totalWords is a multiple of 250 like 17500 then wordsFinalPage will give us 0 and I need it to give 250. How can I do that without using an if statement but instead using a mod function?

View Replies View Related

Positive Value Which Has Multiples Keeps On Displaying

Apr 18, 2015

What ever positive value which has multiples keeps on displaying " No multiples ........ where found, and the answer.how can i fix the colored part ?

import java.util.Scanner;
public class Problem4 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int n;
boolean x=true;
System.out.print("Enter an integer n: ");
n=input.nextInt();

[code]....

View Replies View Related

Program To Detect Number 7 And Its Multiples

Apr 25, 2015

I just started learning Java, I was asked to write a simple program that prints a message if it encounters the number 7 or its multiple.

This should be achievable using simple loops and simple operations...

Here is my attempt:

Java Code:

Scanner scan = new Scanner(System.in);
int i, j, temp;
i = scan.nextInt(); // Awaiting user input
j = scan.nextInt(); // Awaiting user input
if (i > j){ // i = 10 j = 0
temp = i; // temp = 10

[Code]...

Now it works fine for numbers that are less than 70...

Example for output with i = 15, and j = 25:

Java Code:

15
16
!!!
18
19
20
!!!
22
23
24
25 mh_sh_highlight_all('java');

I am not sure what to do in case if for example 'i' and 'j' are very big numbers

I mean, I need it to detect the 7s even if it in the thousands place, actually - no matter how big is the number... So far I only made it to work for numbers that are less than 70...

View Replies View Related

How To Find Biggest Two Int Numbers From Four

May 23, 2014

So, if you are a football/soccer fan, you know that in every FIFA World Cup you have 4 nations. From those 4, 2 will qualify to the Knockout stages.

Let's say, after all the group fixtures are played, the standings are as follows :

1. England - 6 points
2. Spain - 4 points
3. Brasil - 3 points
4. Germany - 1 point

I want to know how I can select ONLY England and Spain for the Knockout stage of the FIFA World Cup (The class I am working on).

View Replies View Related

Find Sum Of Numbers In A Random String?

Jun 17, 2014

Writing a Java program to find sum of numbers in a random string.

(Input=abc235^%$q!12&3 output=250)

If a number has - sign then u should consider it as a negative number.(Input=abc-12t%^&$dcf22 Output=10)

View Replies View Related

Write A Program That Find The Sum Of Two Numbers 62 And 99?

Jan 23, 2015

I have to write a program that find the sum of two numbers 62 and 99 and stores them in a variable named total. However, I have one error that I just can't get rid of and can't tell what it is. I'm using jGrasp and here's what it says:

Programming Challenge #5.java:14: error: class SumofTwoNumbs is public, should be declared in a file named SumofTwoNumbs.java
public class SumofTwoNumbs {
^
1 error
  ----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

and here: is my code:

import java.util.Scanner; 
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
 /**
// This program has variable of several of the integer types.
public class SumofTwoNumbs {
public static void main(String[] args) {

[code]....

View Replies View Related

Program To Find If Numbers Are Consecutive Or Not

Feb 23, 2014

im trying to do a program to find if numbers are consecutive or not! if they are consecutive i need a true as a return and a false when they are not... i did this program and im sure i did something wrong because i keep only true returns ..

Example: (3,5,7) (1,2,2) (7,7,9) should return a false!
Java Code: import java.util.*;
public class Consecutive{
public static void main (String [] args){
Scanner console= new Scanner(System.in);
System.out.println("Enter three numbers");
String numbers = console.nextLine();
System.out.println( "The numbers (" + numbers + ") is '" + consecutive( numbers ) + "'" );
}//end of main

[code]...

View Replies View Related

Code That Find Median Value In List Of Values

Mar 19, 2014

a simple Java program for finding the median value in a list of values with the following requirements:

- Create an array with an even number of values in it (an odd number of values is little bit trickier, so if you want a challenge, do it for either an even or odd number of values)

- Find the value with an equal number of values greater than the value as there are values less than the value

- Your solution must not require a sorted list of values - Output the median value

This assignment is intended to get you to demonstrate basic knowledge of arrays, and to create methods with both input and output.

View Replies View Related

Find Maximum Between Three Numbers Using Ternary Operator?

Aug 23, 2014

Write a program to find maximum between three numbers using ternary operator.

View Replies View Related

Find Arithmetic Mean Of The Numbers Stored In Array

Feb 23, 2014

The program below is intended to find the arithmetic mean of the numbers stored in the array q in two ways: once by storing the numbers in an ArrayList d, where you allow all the necessary conversions to be performed automatically; and once by storing them in an ArrayList e, where you perform all the conversions by hand. Complete the program.

Here is what I have so far:

double[] q = { 0.5, 2.4, 7.4, 2.8, -6.2 };
ArrayList<Double> d = new ArrayList<Double>();
ArrayList<Double> e = new ArrayList<Double>();
for ( double x : q ) {
d.add( x );
e.add ( new Double ( x ) );

[Code] .....

Why does it still show "a / d.size?" I thought I fixed that. Whatever, it's supposed to be "dTotal / d.size()", etc.

View Replies View Related

Logic To Find Adjacency Of Numbers 0 And 1 In Array

Oct 3, 2013

I should mention here that number of elements can be from 1....10'000. And it has to be done as O(N)

I attempted at the problem like so :

public static int adjacencies(int[] A) {
int count = 0;
boolean found = false;
for(int i = 0; i < A.length-1; i++) {
if (A[i] == A[i+1]) {

[Code] .....

To me it looks right and it seems to work.

View Replies View Related

Find Largest Num Of Numbers In Scanned File

Jul 10, 2014

I need to find the largest value in a scanned file.I've gotten the count, sum, average, evens, and odds myself.The code above the while loop is not mine and my professor said I may no edit it or other wise mess with it. I also may not use arrays.Also I've realized that the largest/smallest are recording the value of count. I've tried the following:

Scanner infile = new Scanner ( new FileReader(args[0]) );
int count=0,sum=0, largest=Integer.MIN_VALUE,smallest=Integer.MAX_VALUE, evens=0, odds=0;
double average=0.0;

[code]....

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved