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

Answers were Sorted based on User's Feedback



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

Answer / himanshu

Well this is what I tried to make it more simple, no list...
just checking and calling functions.

int calculate_prime(int prime_no)
{
int check,count=0;
while(prime_no != 0)
{
check=check_prime(prime_no);
if (check == 0)
count++;
prime_no--;
}
return count;
}
int check_prime(int prime_no)
{
int num,prime_check=0;
num=prime_no/2;
while(num > 1)
{
if( (prime_no%num) == 0 )
{
prime_check=1;
break;
}
num--;
}
return prime_check;
}

void main()
{
int a;

//TEST CASE 1:
//PASSING 11 AS ARGUEMENT TO calculate_prime FUNCTION
//PREFERRED OUTPUT SHOULD BE 6
a=calculate_prime(11);
printf("%d",a);

//TEST CASE 2:
//PASSING 10 AS ARGUEMENT TO calculate_prime FUNCTION
//PREFERRED OUTPUT SHOULD BE 5
a=calculate_prime(10);
printf(" , %d",a);


getch();
}

Is This Answer Correct ?    2 Yes 3 No

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

Answer / 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

More Project Planning Interview Questions

Write a function that will return the number of days in a month (not using System.DateTime)?

0 Answers   Microsoft,


create a flow diagram up to 2 levels for hospital management

0 Answers  


What is the stage when code is delivered to the client and he is testing it ?

5 Answers   Microsoft,


How do you handle change requests ?

4 Answers   Microsoft, Trigent,


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

0 Answers   3i Infotech, Choice Solutions, iGate,






You have 3 jars. Each jar has a label on it: white, black, or white&black. You have 3 sets of marbles: white, black, and white&black. One set is stored in one jar. The labels on the jars are guaranteed to be incorrect (i.e. white will not contain white). Which jar would you choose from to give you the best chances of identifying the which set of marbles in is in which jar?

12 Answers   Intel, Microsoft,


What is the team size of your project ?

3 Answers   Infosys, NIIT, Saber Corporation, Satyam,


What are the different phases in Software life cycle

6 Answers  


What procedures do you follow to arrive at the project schedule?

3 Answers  


what is SDLC? what are the different stages in SDLC?

45 Answers   Choice Solutions, College School Exams Tests, HCL, Infosys, SDLC, Soma Enterprises, Wipro,


What is the role of CLR in ASP.NET?

0 Answers  


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

0 Answers  


Categories