answersLogoWhite

0


Best Answer

CLS
FOR n = 2 TO 100
FOR k = 2 TO n / 2
flag = 0
r = n MOD k
IF r = 0 THEN
flag = 1
EXIT FOR
END IF
NEXT k
IF flag = 0 THEN PRINT n,
NEXT n
END

User Avatar

Wiki User

10y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

// Semi-optimized Sieve of Eratosthenes implementation.

// num - determine prime numbers from 0 to num-1

// Returns a boolean array. a[n] = true iff n is prime

public static final boolean[] primesSieveE(final int num) {

// We're going to be performing this particular division a lot,

// so let's do the math once and store the answer for later.

final int num_2 = num / 2;

final boolean a[] = new boolean[num];

// Initially set each value to "true"

Arrays.fill(a, true);

// By definition, 0 and 1 are not prime.

a[0] = false;

a[1] = false;

// To cut down some of the work later on, let's also mark off all multiples of 2.

for (int i = 4; i < num; i += 2) {

a[i] = false;

}

// Now the actual algorithm...

// Loop through each Prime number in our array and mark off all multiples.

// Note that we only need to iterate up through num / 2 because once we get

// past the halfway mark, all multiples of i will be outside the bounds of our array.

for (int i = 3; i < num_2; i += 2) {

// Only mark off multiples if i is prime.

if (a[i]) {

// Mark all multiples of i as composites.

for (int j = i + i; j < num; j += i) {

a[j] = false;

}

}

}

return a;

}

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

count=1

i=3

n=i-1

while count<1000:

if i%n == 0:

i= i+2

n= i-1

else:

n=n-1

while n==1:

print i

count=count+1

i= i+2

n= i-1

while count==1000:

print (i-2)

break

print "1000th prime is ", i-2

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

A for loop is simpler in this case; though of course, any for loop can be emulated with a while loop. Actually, you need two nested for (or while) nested loops. The outer one loops through all the numbers from 1 to 1000; the inner one is used to check all the factors, from 1 to (number being tested minus 1). For illustration purposes, when you are checking the number 6, you check whether 1, 2, 3, 4 and 5 are factors, and if they are, you add them to the sum of factors, which you must of course initialize to zero. In this example, the factors are 1, 2, and 3, so the sum is 6.

If the sum is equal to the number being tested, you print it out. This requires an if.

You didn't specify what computer language you want to use; the pseudocode would look something like the following - adapt it to your favorite language (I am using underlines for indentation, otherwise the indentation would be lost):

for number = 1 to 1000

_ sum = 0

_ for factor = 1 to number - 1

_ _ if number % factor = 0

_ _ _ sum += factor

_ if number = sum

_ _ print number

To change the for loops to while loops (not convenient, except as an academic exercise), initialize the variable before the while, and don't forget to increment it at the end. Thus, the outside loop would be:

number = 1

while number <= 1000

_ (main body of loop here)

_ number++

This answer is:
User Avatar

User Avatar

Wiki User

16y ago

If you wanted to output numbers in PHP to get less than a thousand and display all those numbers you could put it in a while loop like this.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a java program that finds and displays all the prime numbers less than 1000?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Make a C programming that displays the first four perfect numbers?

create a program that iterates until it finds a perfect number, then store that perfect number into an array. Continue iterating until you find three more. Then, you have an array of four perfect numbers.


Write a C program that takes a binary file as input and finds error check using different mechanisms?

write a c program that takes a binary file as input and finds error check using different mechanisms.


What is the pseudo code for a program that finds the product of two numbers?

int x; //first number int y; //second number int z = x*y;


What is the linux program that finds and repair disk error?

fsck


How do you create Write a program that takes 10 integers as input The program will place these integers into an array and display the following 1. List in ascending order 2. mean 3. median 4.max val?

import java.util.Arrays; import java.util.Scanner; public class Answers { public static void main(String[] args) { //Creates a scanner object named console. Scanner console = new Scanner(System.in); //Variabels int [] numbers = new int [10]; double avg = 0.0; double median = 0.0; int max = numbers[0]; double count = 0.0; //User input. for (int i = 0; i &lt; numbers.length; i++){ System.out.print("Number: "); numbers[i] = console.nextInt(); } //break System.out.println("==============="); //finds the average and max value. for (int i = 0; i &lt; numbers.length; i++){ count += numbers[i]; avg = count / numbers.length; //average if (numbers[i] &gt; max){ //finds the max value. max = numbers[i]; } } median = (numbers[4] + numbers[5])/2; //Median value //Display to user. System.out.println("Highest value found: " + max); //Show maximum value found in array System.out.printf("Median is: %.3f \n",median); //Show median System.out.printf("Average is: %.3f \n",avg); //Show average sortAsc(numbers); //Print out whole array ascending } //Method for sorting an Array ascending. public static void sortAsc(int [] array){ for (int i = 0; i &lt; array.length; i++){ Arrays.sort(array); System.out.println(array[i]); } } } This should do everything you asked for, hope this helps!

Related questions

Make a C programming that displays the first four perfect numbers?

create a program that iterates until it finds a perfect number, then store that perfect number into an array. Continue iterating until you find three more. Then, you have an array of four perfect numbers.


Write a C program that takes a binary file as input and finds error check using different mechanisms?

write a c program that takes a binary file as input and finds error check using different mechanisms.


How do you compile and execute a java program which finds out the highest of any five numbers?

it will be destroyed.


Write a program that takes a binary file as input and finds error check using different mechanism?

networking


Which command line tool finds and displays objects in active directory that meet specified criteria?

DSQUERY


What is the pseudo code for a program that finds the product of two numbers?

int x; //first number int y; //second number int z = x*y;


HOW TO SOLVE 'C' problem?

1. write a 'c' program to read 4(four)numbers from a file 'BANK' and calculate the average of the numbers.Now print the calculated average in another output file 'AVERAGE' 2. write a 'c' program that finds the sum and average of inputted five integer numbers of the array using dynamic memory allocation function malloc(). 3. write a 'c' program to create simple elements 1,2,3,4 in the link list of 4(four)nodes and display the list's elements. 4. write a 'c' program to convert the expression (A+B)/(C+D) into postfix expression into stack.and then evaluate it for A=10,B=20,C=15,D=5 and display the stack status after each operation. 5. write a 'c' programto create a linked list implemented on an array containing the following numbers:1,2,3,3,3,4,4,9 and pack it to remove the duplicate numbers.so that only the following data are contained by the nodes:1,2,3,4,9


What is the linux program that finds and repair disk error?

fsck


Is there a program that finds a selected color and clicks on it?

Impossible sorry. :(


What object finds and displays selected data?

Say you want to display A1. You would put into the cell =A1.


What object finds and displays selected data in Microsoft access?

Say you want to display A1. You would put into the cell =A1.


What are the release dates for The Jack Benny Program - 1950 Jack Finds a Double 15-23?

The Jack Benny Program - 1950 Jack Finds a Double 15-23 was released on: USA: 12 March 1965