Click here to Skip to main content
15,887,264 members
Please Sign up or sign in to vote.
1.16/5 (4 votes)
See more:
Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate all prime numbers between two given numbers!


Input : The input begins with the number t of test cases in a single line (t<=10). In each of the next t lines there are two numbers m and n (1 <= m <= n <= 1000000000, n-m<=100000) separated by a space.

Output : For every test case print all prime numbers p such that m <= p <= n, one number per line, test cases separated by an empty line.

Example

Input:
2
1 10
3 5

Output:
2
3
5
7

3
5

My answer to the question is

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = Convert.ToInt32(Console.ReadLine());
            int b = Convert.ToInt32(Console.ReadLine());
 
            for(int i= a;i<= b;i++)
            {
                int k = 0;
                {
                    for (int j = 1; j <= i; j++)
                    {
                        if (i % j == 0)
                        {
                            k++;
                        }
                    }
                    if (k == 2)
                    {
                        Console.WriteLine(i);
                    }
                }
            }
            Console.ReadLine();
            }
 
        }
    }


this renders perfectly the prime numbers of any range between the two specified numbers while running in VS2012.
Posted
Updated 11-May-15 19:05pm
v2
Comments
Sergey Alexandrovich Kryukov 12-May-15 0:09am    
Please use the debugger.
What's the question? "Renders perfectly" or "generates run time error".
Tell Peter to ask his questions by himself. :-)
—SA
Mehdi Gholam 12-May-15 0:19am    
I believe Peter to be busy studying :)
Sergey Alexandrovich Kryukov 12-May-15 0:21am    
:-)
Than it's better not to answer until Peter attends this page.
—SA
Member 1097736 2-Jun-15 13:48pm    
sir i am new to programming.This is a question asked in SPOJ. As far up to my understanding we need to generate prime nos between two numbers.I have written code for that. I want you to assess whether my code answers the question.If not,i want to know what is the logic behind it.
PIEBALDconsult 12-May-15 1:11am    
Please rethink. Hint: in the above case, once you've found the primes from 1 to 10 -- you also have the primes from 3 to 5 -- don't throw them out!
And read up on http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes .

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900