Write a function that counts the number of primes in the
range [1-N]. Write the test cases for this function.

Answer Posted / softwareguy

This is what I came up with (C#):

PSEUDO:
-Craete a list of numbers uptill n number - n being int
parameter
-Pass this list into a function to check for primilaty for
the number and then print primes wile we count for primes

BACKGROUND OF THIS SOLUTION:

THEORY:
- we know primes are the numbers starting from 2 are
divisible with 1 and themselves only i.e. n being a prime
number can only be divisble as n/n or n/1. testing from a
prime is called "primilatiy test" for a number...
-the simplest primality test is to see if given a number n
is divisible by an integer m from 2 to n-1. If n is
divisible by n then n is a composite number otherwise its a
prime...

TO SPEED UP THE COMPUTING WE CAN ALSO:
Rather than testing till n-1 we can test the number till
Square Root of n i.e. if n is a prime number it can always
be factored into 2 values.

REFERENCE:
See here if you want to be a mathematician ;-).
http://en.wikipedia.org/wiki/Primality_test

THE CODE:

I would assume you know how to deal with lists in c# so I
will not get into that. Just create an integer based items
list "list<int>" etc. in C# which adds digits to the list
till number n. HINT: use a for loop ...LOL!!

public void CountPrimes(list<int> c)
{
list<int> primes = c.FindAll(
delegate(int a)
for (int i = 2; i <= Math.Sqrt(a); i++)
{
if (a % i == 0)
return false; //is not a prime
return true;//is a prime
}
//list primes in a list box and get the total count
label1.Text = "Total Primes = " + Convert.ToString
(p.count);
for (int count = 2; count < p.count; count++)
{
listbox.Items.Add(Convert.ToString(p[i]));
}
}

and you are done!!! let me know if this is good enough...

Is This Answer Correct ?    1 Yes 4 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Did you used crystal reports? Do you have sufficient knowledge on crystal reports?

2160


Write the test cases for instr function?

648


Explain dll hell?

667


Please, i am a final year student in nigeria and i have a project on pension fund administration and i need help in starting.

1880


Why do you want to work for Microsoft?

2716






Tell about yourself and job ?

2089


How would you deal with a bug that no one wants to fix? Both the SDE and his lead have said they won?t fix it?

2882


How do you create the Software Project Management Plan (SPMP)?

4045


How do you ensure that your project plan is available for others to see? Where will you find the plans of other projects executed (in the past or currently) in the center?

2042


I want question and paper based MS Projects. Also is there any exam which microsoft conduct for Ms Project professional to issue certification?

2175


How are the team members kept informed about the current status of the project?

2180


Assignment A software house got a project to automate sales/purchase department of a furniture store. The store management is clearly transferred their requirements to the software house management. There is no chance of changing the requirements afterwards. Software house management use waterfall process model to make the project. Given is the progress flow of water-fall process model In waterfall process model, the deliverable (document) produced in one phase serves as an input to the next phase. Suppose you are a project manager who is leading this project. It is decided to work and complete the project using waterfall process model approach. You are required to develop a plan in which the key task is to decide the deliverable that should be built during and after completion of each phase. Also give a description or required contents that must be included in those deliverables. You are not supposed to make the complete project. All you are required to do is to make a Project plan. In that, you need to give a set of deliverables (documents) which according to you, should be developed in each of the phase (Requirements, Designing, implementation, Testing, Maintenance). Also provide contents which should be there in each deliverable. The contents should be clear and have brief explanation. Note:: There are one folder and one file in the attached folder. The file is named as Assignment 4 write-up which is your assignment write-up. The other folder named Project deliverables is the folder in which you are required to organize and place your deliverables. Name of the document/deliverable saved in each folder should be mentioned in the file named checklist (This file is placed in the “Project deliverables” folder) For Example If you produce a deliverable named Requirement Specification (RS) in the “Requirements” phase. You need to do following tasks 1) Finalize the RS contents 2) Save the file in sub-folder named Requirements 3) Write the name of this document in the checklist file under the heading Requirements. Similarly, you need to make and save deliverables of each phase. Not all phases contain one deliverable. There must be some phases having more than one deliverables. PLEASE SIR PRIVIDE ME THE CHECK LIST FOR GIVE SENERION ? CHECKLIST ARE: 1-Requirements 2- Designing 3- Implementation 4- Testing 5- Maintenance

6489


funds flows analysis how to prepare for interview

1045


How do you rate yourself in c#,vb.net,Asp.net,oracle,sql server ?

4976


Tell about current project ?

2220