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

  • Context: C/C++ 
  • Thread starter Thread starter sandy.bridge
  • Start date Start date
  • Tags Tags
    C++ Code
Click For Summary

Discussion Overview

The discussion revolves around the significance and functionality of a C++ code snippet, specifically focusing on its role in identifying prime numbers through an algorithmic approach. Participants analyze the code's structure and purpose, providing varying levels of detail in their explanations.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested

Main Points Raised

  • One participant describes the code as scanning an array to track properties of its elements, assigning Boolean values based on certain conditions.
  • Another participant identifies the algorithm as the sieve of Eratosthenes, which is used to find prime numbers.
  • A subsequent reply questions the completeness of the initial explanation, suggesting that it lacks detail about the process of marking multiples of integers.
  • A later response clarifies that the code marks multiples of each integer starting from 2, ultimately leaving only prime numbers in the table.

Areas of Agreement / Disagreement

Participants express differing views on the adequacy of the initial explanation, with some asserting that it does not fully capture the algorithm's function. There is no consensus on the completeness of the explanations provided.

Contextual Notes

The discussion highlights varying interpretations of the code's purpose and the algorithm's mechanics, with some assumptions about the reader's familiarity with the sieve of Eratosthenes not being explicitly stated.

Who May Find This Useful

Individuals interested in algorithms, programming in C++, or number theory may find this discussion relevant.

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.
 

Similar threads

  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 22 ·
Replies
22
Views
4K
Replies
1
Views
2K
Replies
12
Views
2K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 34 ·
2
Replies
34
Views
6K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 31 ·
2
Replies
31
Views
3K