Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


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

How does the senior management review your project's progress?

1 Answers   Wipro,


What is the document that should be consulted to know about your project, the activities you do, your schedules and milestones?

7 Answers  


Scenario: -------- -- You are a project manager in a large software house of 375 staff in total. The company has been running for 5 years. The company has been CMM assessed at level 3. The company has many external clients for whom it develops a wide range of business applications. -- Most of the company‘s applications are developed in Java. The waterfall lifecycle development model is normally used in developments. -- The company has historically suffered from the type of problems that are common in the software industry. In particular systems have been delivered not always to user requirements and containing faults. The managing director of the company is very keen for improvements to be made in these areas. -- You are in charge of a project team of 8 developers, 2 requirements engineers and 3 testers. Your team is just about to start a new development. System to Develop :( Exact Question ) ------------------------------------- -- Your team is about to start to develop a novel vehicle navigation system for physically impaired drivers. The navigation system not only identifies the correct route for the driver in the normal way, but also controls the steering wheel to navigate the car to the destination identified by the driver. -- A basic statement of requirements has been received from the customer and budget and timescales have been formalised into a contract. Requires TASK to write : ------------------------ -- You must write a short report (maximum of 1 page) for the managing director of your company outlining how you propose to ensure that this project improves on previous projects in terms of delivering to user requirements and without faults. Your report must include how you propose to show improvement on these issues to your managing director. Also please tell me how should I choose the best software model when given scenarios like these ...?

1 Answers  


funds flows analysis how to prepare for interview

0 Answers   Mphasis,


How do you track the technical activities in your project? How is the status of the project communicated to the team?

3 Answers  


Explain dll hell?

0 Answers  


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

0 Answers   Microsoft,


Why do you want to work for Microsoft?

0 Answers   Infosys, Microsoft,


Does XML has white spaces?

1 Answers  


How do speedup the project delivery without affecting the cost?

6 Answers   emc2, HCL,


Write the test cases for instr function?

0 Answers  


Where and how are the risks associated with your project identified and documented?

2 Answers  


Categories