Click here to Skip to main content
15,916,941 members
Home / Discussions / C#
   

C#

 
GeneralRe: Menustrip Selfmade Pin
Robert Rohde21-May-05 5:00
Robert Rohde21-May-05 5:00 
GeneralC# code to find Prime Numbers Pin
diabolusgorgon21-May-05 4:33
diabolusgorgon21-May-05 4:33 
GeneralRe: C# code to find Prime Numbers Pin
Robert Rohde21-May-05 4:46
Robert Rohde21-May-05 4:46 
GeneralRe: C# code to find Prime Numbers Pin
Asad Hussain21-May-05 10:43
Asad Hussain21-May-05 10:43 
GeneralRe: C# code to find Prime Numbers Pin
diabolusgorgon21-May-05 13:31
diabolusgorgon21-May-05 13:31 
GeneralRe: C# code to find Prime Numbers Pin
Robert Rohde21-May-05 20:04
Robert Rohde21-May-05 20:04 
GeneralRe: C# code to find Prime Numbers Pin
diabolusgorgon21-May-05 13:27
diabolusgorgon21-May-05 13:27 
GeneralRe: C# code to find Prime Numbers Pin
Robert Rohde21-May-05 20:37
Robert Rohde21-May-05 20:37 
Ok, as its Sunday and I have nothing really important to do...

public ArrayList FindPrimes(int maxNumber) 
{
	ArrayList result = new ArrayList();
	for (int i = 3; i <= maxNumber; i += 2)
		if (IsPrime(i))
			result.Add(i);
	return result;
}

public double GetPrimesAverage(int maxNumber)
{
	return CalcAverage(FindPrimes(maxNumber));
}

public bool IsPrime(int number) 
{
	double max = Math.Sqrt(number);
	for (int i = 3; i <= max; i += 2)
		if (number % i == 0)
			return false;
	return true;
}

public double CalcAverage(ArrayList list) 
{
	double sum = 0;
	foreach (int number in list)
		sum += number;
	return sum / list.Count;
}


Generally its the the algorithm I already stated (including some optimizations I initially didnt want you to take care of Smile | :) ).
I tried to split up the functionality. The function names should be descriptive enough to tell you want each part is doing. Note that IsPrime gives only valid results for uneven numbers.

As GetPrimesAverage gets an argument you can calculate the average for as much prime numbers as you want (cpu is the limit Smile | :) - dont test it with more than about 20 millions). To get the result for a maximum of 100 the following call had to be done:
GetPrimesAverage(100)

Do yourself the favor and read it carefully until you understand it. If you already know how to use a debugger it would be good if you just step through the code until you get it.
QuestionHow to include HTML Pin
Murtuza Husain Miyan Patel21-May-05 4:13
professionalMurtuza Husain Miyan Patel21-May-05 4:13 
AnswerRe: How to include HTML Pin
DavidNohejl21-May-05 4:22
DavidNohejl21-May-05 4:22 
Generalobfuscator/urgent!!! Pin
rabi kumar21-May-05 1:39
rabi kumar21-May-05 1:39 
GeneralListbox Context Menu Pin
Da`Sooty20-May-05 23:06
Da`Sooty20-May-05 23:06 
GeneralRe: Listbox Context Menu Pin
S. Senthil Kumar21-May-05 0:16
S. Senthil Kumar21-May-05 0:16 
GeneralRe: Listbox Context Menu Pin
Robert Rohde21-May-05 0:26
Robert Rohde21-May-05 0:26 
GeneralProblem reading stream Pin
AnonymousTwo20-May-05 22:50
AnonymousTwo20-May-05 22:50 
GeneralRe: Problem reading stream Pin
Robert Rohde21-May-05 0:48
Robert Rohde21-May-05 0:48 
Generaljoin DBF files Pin
Bedevian20-May-05 20:07
Bedevian20-May-05 20:07 
GeneralRe: join DBF files Pin
Robert Rohde20-May-05 20:56
Robert Rohde20-May-05 20:56 
GeneralRe: join DBF files Pin
Bedevian20-May-05 23:56
Bedevian20-May-05 23:56 
GeneralRe: join DBF files Pin
Robert Rohde21-May-05 0:15
Robert Rohde21-May-05 0:15 
GeneralRe: join DBF files Pin
Bedevian21-May-05 19:34
Bedevian21-May-05 19:34 
GeneralRe: join DBF files Pin
Robert Rohde21-May-05 20:02
Robert Rohde21-May-05 20:02 
GeneralRe: pass dataset to new database Pin
sevan22-May-05 21:03
sevan22-May-05 21:03 
GeneralShortcut key for C# Application Pin
Anonymous20-May-05 20:05
Anonymous20-May-05 20:05 
GeneralRe: Shortcut key for C# Application Pin
ekynox20-May-05 22:47
ekynox20-May-05 22:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.