[英]Print out prime Number less than a given number N
Print out the prime numbers less than a given number N. For bonus points your solution should run in N*log(N)
time or better. 打印出少于给定数字N的质数。要获得加分,您的解决方案应在N*log(N)
时间或更长时间内运行。 You may assume that N is always a positive integer. 您可以假设N始终是一个正整数。
Input sample: 输入样本:
Your program should accept as its first argument a path to a filename. 您的程序应将文件名路径作为其第一个参数。 Each line in this file is one test case. 该文件中的每一行都是一个测试用例。 Each test case will contain an integer n < 4,294,967,295
. 每个测试用例将包含一个整数n < 4,294,967,295
。
Eg 例如
10
20
100
Output sample: 输出样本:
For each line of input, print out the prime numbers less than N, in ascending order, comma delimited. 对于输入的每一行,以升序打印输出小于N的质数,以逗号分隔。 (There should not be any spaces between the comma and numbers) Eg (逗号和数字之间不应有空格)例如
2,3,5,7
2,3,5,7,11,13,17,19
2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97
Here is my solution: 这是我的解决方案:
public class problem1 {
public static void main(String [] args) throws Exception
{
File f=new File("C://Users/Rahul/Documents/Projects/r.txt");
FileReader fr=new FileReader(f);
List<Integer> l=new ArrayList<>();
int p;
BufferedReader br = new BufferedReader(fr);
String s;
while( (s= br.readLine()) != null ) {
int a=Integer.parseInt(s);
for(int i=2;i<a;i++)
{
p=0;
for(int j=2;j<i;j++)
{
if(i%j==0)
p=1;
}
if(p==0)
l.add(i);
}
String st=l.toString();
st=st.replaceAll("\\[", "").replaceAll("\\]", "").replace(", ", ",");
System.out.print(st);
System.out.println("\t");
}
fr.close();
}
}
My input is : 我的输入是:
10
50
And output is : 输出为:
2,3,5,7
2,3,5,7,2,3,5,7,11,13,17,19,23,29,31,37,41,43,47
But when i submit this solution they are not accepting this solution. 但是,当我提交此解决方案时,他们不接受此解决方案。
But when i put content in document like this: 但是当我将内容放在这样的文档中时:
10 50
30
I am trying that java program ignore this 50. How to do it ? 我正在尝试该Java程序忽略此50。该怎么做?
Any better solution then this ? 还有更好的解决方案吗? Give me some idea! 给我一些想法!
To ignore the extra number in your file you can take only the first number of each line. 要忽略文件中的多余数字,您只能使用每行的第一个数字。
Your solution is probably not accepted because in your second line you have printed 2,3,5,7
twice (ie the primes of the previous line) 您的解决方案可能不被接受,因为在第二行中,您已经两次打印了2,3,5,7
(即前一行的素数)
See the example below to fix both problems 请参阅下面的示例来解决这两个问题
while( (s= br.readLine()) != null ) {
String [] numbers = s.split(" "); // split the line
int a = Integer.parseInt(numbers[0]); // take only the first one
....
System.out.print(st);
System.out.println("\t");
l.clear(); // clear the list before trying to find primes for the new line
}
"Your program should accept as its first argument a path to a filename" “您的程序应将文件名路径作为第一个参数”
You have a hardcoded filename in your solution - use args[0]
instead. 解决方案中包含硬编码的文件名-请改用args[0]
。
Othwerwise, your solutions looks OK, although there is some room for improvements regarding the efficiency. 否则,您的解决方案看起来还可以,尽管在效率方面还有一些改进的余地。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.