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
// 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;
}
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
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++
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.
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.
int x; //first number int y; //second number int z = x*y;
fsck
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 < 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 < numbers.length; i++){ count += numbers[i]; avg = count / numbers.length; //average if (numbers[i] > 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 < array.length; i++){ Arrays.sort(array); System.out.println(array[i]); } } } This should do everything you asked for, hope this helps!
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.
it will be destroyed.
networking
int x; //first number int y; //second number int z = x*y;
DSQUERY
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
Say you want to display A1. You would put into the cell =A1.
fsck
Impossible sorry. :(
Say you want to display A1. You would put into the cell =A1.
Just write a loop that goes through "candidates" (for example, numbers from 2 to 100). To check whether each number is a prime number, write a second loop that checks whether it is divisible by all numbers from 2 up to the number itself. If the first factor you thus find is the number itself, then it is a prime number. For example, in Java:...for (i=2; i