answersLogoWhite

0

#include<stdio.h>

bool is_prime (unsigned n) {

if (n<2) return false;

if (!(n%2)) return n==2;

unsigned max_factor = (unsigned) sqrt (n) + 1;

unsigned factor;

for (factor=3; factor<max_factor; ++factor) {

if (!(n%factor)) return false;

}

return true;

}

unsigned next_prime (unsigned n) {

while (!is_prime (++n));

return n;

}

int main() {

/* print the first 100 prime numbers */

unsigned i, n=0;

for (i=0; i<100; ++i) {

n = next_prime (n);

printf ("%d\n", n);

}

return 0;

}

User Avatar

Wiki User

9y ago

What else can I help you with?

Continue Learning about Engineering

How do you write a program in lisp to find if a number is prime or not?

(defun prime (num) (if (&lt; 2 num) (do ((dividend 2 (1 + dividend)) (chk-to (sqrt num))) ((equal (rem num dividend) 0)) (when (&lt;= chk-to dividend) (return t))) t))


Write a c program to find out the prime numbers between 1 to 500?

To write a C program to find prime numbers between 1 to 500, you can use a nested loop structure. In the outer loop, iterate from 2 to 500, and in the inner loop, check if the number is divisible by any number from 2 to the square root of the number. If it is not divisible by any number other than 1 and itself, then it is a prime number. Print out all prime numbers found within the specified range. Remember to include necessary header files, such as &lt;stdio.h&gt;, and use appropriate logic to implement the program efficiently.


How do you write Square program using vb?

write a vb program to find the magic square


Writes a c program to find the sum of all integers between 1 and n?

Write a program to find the number and sum of all integers from 100 to 300 that are divisible by 11


How do you write a program in Python to find the first n prime numbers?

One way to do this is to write a function that checks if a number is prime: def isPrime(number): for i in range(2, number): if number%i == 0: return False return True Then create variables to track how many primes have been found and a variable to track which number is being tested for being prime. Increment that variable and test it, and if it is prime print it out (or save it somewhere) and increment the variable being used to track how many primes have been found: targetPrimes = 10 #number of primes to find primesFound = 0 #number of primes found i = 1 while (primesFound &lt; targetPrimes): i += 1 #first number tested is 2 if isPrime(i): print(i) primesFound += 1

Related Questions

What is the largest prime no that is stored in 8 bit pattern?

Write your own prime number program and find out.


Write a c program to find given number is prime or not?

Yes, do write, or if you're too lazy to your homework, use google.


How do you write a VBnet program to find the prime numbers between 100 to 200?

VBnet program to find the prime numbers between 100 to 200?


With a given big integer number which is the product of two prime numbers how do you find them?

First write a program to generate the prime number. After one prime number was generated, divide the big int number by the prime number. If the remainder is zero then quotient is the second prime number ( also it is important to check whether the quotient is prime number or not because sometimes you will get wrong answer). Repeat the process until you get the result.


write a pseudocode to accept a number and find if it is prime or not?

Yes


A program to find that the input number is prime or not?

Use Wolfram|Alpha... go to the related link below, Wolfram|Alpha, and type in (is __ (number) prime) and then the program will compute that and tell you if it is prime or composite.


Write a C program to find the sum of all prime numbers?

Since there is an infinite set of prime numbers the answer would be infinity.


Prime numbers between 1 to 10 in microprocessor 8085?

program to find prime number in 8085 microprocessor


Write a Shell program to find the smallest number from a set of numbers?

k


Write a java programme to check whether the number is twin prime or not?

Find a prime number, add 2 to the number. Check if the new number is prime. IE : 3 is prime. 3+2 =5. 5 is prime. (3,5) are twin primes.


How do you write a program to find whether the given number is prime or not and show output?

#include&lt;stdio.h&gt; #include&lt;conio.h&gt; void main() { int n,a=2; clrscr(); printf("\n enter n"); scanf("%d",&amp;n); if(i&lt;=a-1) { if(a%i==0) { printf("\n the given number is not a prime number"); break; } i++; if(a==i) { printf("\n the given number is a prime number"); } getch(); output: enter the value of n:2 the given number is prime number


How do you write a program in lisp to find if a number is prime or not?

(defun prime (num) (if (&lt; 2 num) (do ((dividend 2 (1 + dividend)) (chk-to (sqrt num))) ((equal (rem num dividend) 0)) (when (&lt;= chk-to dividend) (return t))) t))