Please anyone tell me coding to print prime numbers from 1 to
5000 in c#....

Answer Posted / slau

In PHP, translate C# yourself
//array for prime numbers
$primes=array(2); //speed trick - we know that 2 is prime
for ($i=3;$i<=5000;$i+=2) { //speed - check only odd numbers
//max. possible divisor for number
$maxdiv = sqrt($i);
for ($j=0; $j<count($primes); $j++) {
// if modulus = 0, number has divisor
if (0 == $i % $primes[$j]) {
break;
}
//if all possible prime divisors checked, add new prime
if ($primes[$j] > $maxdiv) {
$primes[] = $i;
echo "$i\n";
break;
}
}
}


not very brilliant, 10 mins for coding, but runs pretty fast

Is This Answer Correct ?    2 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

could u please also write an example of a code that involves instances from an abstract class just as u did for interfaces because u said it could also work which i really doubt. thanks

1629


through which algorithm does the garbage collector works? how the garbage collector will understand that the object will going to be deleted?

2078


THERE ARE 4 SOURCE FILES WHICH CONTAINS SAME METADATA CREATE A TARGET THAT SHOULD DISPLAY THE FILE NAME ALONG WITH THE RECORD PLEASE SEND THE ANSWERFOR THIS QUESTION WITH MAPPING

1690


can u send some model papers how computer awareness questions are asked in goverment jobs(oriental insurance)to my mail id me.priyankapadma@rediff.com

1939


Hi can you please send me recent(present) interview questions and technical qyestions with answers for "BUSSIESS OBJECTS" and "DATA WAREHOUSE".pls its urgent for me my mail id is sekhar.cs82@gmail.com,manjuforgis@gmail.com. thanks in advance

1495






how to get second highest salary from a employee table and how get a 5th highest salary from a employee table by using proc sql?

1902


Busy waiting is a method whereas a taskwaits for a given event by continiously checking for an event to occur. What is the main problem with this approach

1416


Given a set. Write the pseudo code to get all the subsets for the given set. Eg. Input : {1,2} Output : (),(1),(2),(1,2)

2238


Is buffer size and file block size is similar? If similar,at which case it will be same size?

1742


What is the difference between DECISION COVERAGE(DC) and MODIFIED CONDITION/DECISION COVERAGE(MCDC)?

1788


Which CRM is better to opt, Sebiel CRM or Salesforce CRM or sap CRM? which institute is best one in hyderabad

1856


Find out the roles which gives access to all tables in SAP? Thanks in advance.

1568


how can we implement locks in plsql?

1803


19. Given a system that is described with the following equation, X=A+(B.(A̅+C)+C)+A.B.(D̅+E̅) a) Simplify the equation using Boolean Algebra. b) Implement the original and then the simplified equation with a digital circuit. c) Implement the original and then the simplified equation in ladder logic.

1252


difference between mantis and other tools?

1624