C/C++ What is the significance of this C++ code and what does it do?

  • Thread starter Thread starter sandy.bridge
  • Start date Start date
  • Tags Tags
    C++ Code
Click For Summary
The discussion centers around a code snippet implementing the Sieve of Eratosthenes algorithm, which is used to find prime numbers up to a specified limit, in this case, 15. The code initializes a boolean array to track the primality of numbers, marking all numbers as potential primes. It then iteratively marks the multiples of each prime starting from 2 as non-prime (false). The final output displays the remaining true values in the array, which correspond to the prime numbers. The explanation emphasizes the algorithm's efficiency in identifying primes by systematically eliminating non-prime numbers through marking.
sandy.bridge
Messages
797
Reaction score
1
Hey guys. I just want to see if I am indeed "seeing" the significance of the code that is to follow. I will follow it with a brief explanation of what I thought the code did. Thanks in advance.


PHP:
	const int N = 15;    
	bool table[N];

	int i = 0;
	while (i < N)
	{
		table[i] = true;
		i = i + 1;
	}

	table[0] = false;
	i = 2;
	while (i < N)
	{
		if (table[i])
		{
			int j = 2*i;

			while (j < N)
			{
				table[j] = false;
				j = j + i;
			}
		}
		i = i + 1;
	}

	i = 0;
	while (i < N)
	{
		if (table[i])
		{
			count << i << " ";
		}
		i = i + 1;
	}
	count << endl;

This program scans through the program in accordance to properties exhibited by the integer value i. If the element in the array had a property being tracked, then it was assigned Boolean true. If it was not, then it was assigned Boolean false. This program demonstrates how one can scan through an array, pursuing a particular property, and henceforth convey which elements within that array had that property.
 
Technology news on Phys.org
You're missing the forest for the trees. This is a simple algorithm to find the prime numbers. It's called the sieve of Eratosthenes.
 
That is the end result; but are any details in my explanation off?
 
Well you have not explained what it does. It makes a table of numbers. It marks multiples of 2, then multiples of 3, then multiples of 4, then multiples of 5, etc. Eventually only prime numbers remain.
 
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 25 ·
Replies
25
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
12
Views
2K
Replies
1
Views
2K
Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 30 ·
2
Replies
30
Views
4K