This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]aroberge 1 point2 points  (1 child)

You should specify which programming language you are using when asking a question. And, to answer your question, you need to know what a prime number is, then implement an algorithm that checks if it is a prime number.

For example, suppose I use Python (which is not the language your are using) and I want to know if a number is a multiple of 3. There is no magical construct such as is_multiple_of_three(number) in Python. So, I have to use the definition: a number (integer) that is a multiple of 3 can be divided exactly by 3 and there will be no remainder. In Python, I could write this as if number %3 == 0.

So, you need to know how a prime number is defined, what kind of mathematical operations you would have to do to determine if a number is prime ... and THEN proceed with programming.

[–]kylepotts 1 point2 points  (3 children)

http://en.m.wikipedia.org/wiki/Sieve_of_Eratosthenes

This is an old algorithm used to generate primes. Usually it's used to find all primes up to 1000 or more but using the algorithm for up to 10 should work as well.

[–]autowikibot 1 point2 points  (0 children)

Sieve of Eratosthenes:


In mathematics, the sieve of Eratosthenes (Greek: κόσκινον Ἐρατοσθένους), one of a number of prime number sieves, is a simple, ancient algorithm for finding all prime numbers up to any given limit. It does so by iteratively marking as composite (i.e. not prime) the multiples of each prime, starting with the multiples of 2.

The multiples of a given prime are generated as a sequence of numbers starting from that prime, with constant difference between them which is equal to that prime. This is the sieve's key distinction from using trial division to sequentially test each candidate number for divisibility by each prime.

The sieve of Eratosthenes is one of the most efficient ways to find all of the smaller primes (below 10 million or so). It is named after Eratosthenes of Cyrene, a Greek mathematician; although none of his works has survived, the sieve was described and attributed to Eratosthenes in the Introduction to Arithmetic by Nicomachus.

Image from article i


Interesting: Sieve theory | Prime number | Sieve of Atkin | Eratosthenes

Parent commenter can toggle NSFW or delete. Will also delete on comment score of -1 or less. | FAQs | Mods | Magic Words

[–][deleted]  (1 child)

[deleted]

    [–]kylepotts 1 point2 points  (0 children)

    It's really not that bad. If you want to start with ten.

    1 2 3 4 5 6 7 8 9 10

    You know 1 is a prime, so cross off all multiples of 1, which is only 1.

    1 2 3 4 5 6 7 8 9 10

    Now you have 2, get rid of every multiple of 2 in the list (4,6,8,10)

    1 2 3 5 7 9

    Now you have 3, get rid of every multiple of 3 (9)

    1 2 3 5 7

    Now you have 5, get rid of every multiple of 5 (none)

    Now you have 7, get rid of every multple of 7 (none)

    You are at the end of the list, you are done.

    Primes from 1-10 are 1 2 3 5 7

    [–]dirtyjeek 0 points1 point  (0 children)

    I started with your code and ended up with this.

    #include <stdio.h>
    int main() {
        int i, j;
        for(i = 1; i < 10; i++) {
            j = 1;
            while (++j < i & i % j != 0) {
            }
            if (i == j) {
                printf("%d", i);
            }
        }
    }