Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,038 questions

40,849 answers

573 users

How to calculate the 1000st prime number in C

2 Answers

0 votes
#include <math.h>
#include <stdio.h>
#include <stdbool.h>
 
bool isPrime(int n) {
    if (n == 0) return false;
    if (n == 1) return false;
 
    for (int i = 2; i <= sqrt(n); i++) {
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}
 
int main(void) {
    int i = 0;
    int prime = 2;
 
    while (i < 1000) {
        if (isPrime(prime)) {
            i++;
        }
        prime++;
    }
    
    printf("%d ", --prime);
}
 
 
 
 
/*
run:
 
7919
 
*/

 





answered Oct 21, 2023 by avibootz
0 votes
#include <math.h>
#include <stdio.h>
#include <stdbool.h>
 
bool isPrime(int n) {
    if (n == 0) return false;
    if (n == 1) return false;
 
    for (int i = 2; i <= sqrt(n); i++) {
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}

int getTheNthPrimeNumber(int N) {
    int i = 0;
    int prime = 2;
 
    while (i < N) {
        if (isPrime(prime)) {
            i++;
        }
        prime++;
    }
    
    return --prime;
}
 
int main(void) {
    printf("%d ", getTheNthPrimeNumber(1000));
}
 
 
 
 
/*
run:
 
7919
 
*/

 





answered Oct 21, 2023 by avibootz

Related questions

1 answer 37 views
1 answer 31 views
31 views asked Oct 21, 2023 by avibootz
1 answer 26 views
1 answer 166 views
...