简体   繁体   English

打印出素数小于给定数N

[英]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.

相关问题 最大数素数小于给定数java - the biggest number prime less than a given number java 给定数字n,我们必须找出小于或等于n的正好有3个除数的数字 - given a number n , we have to find out such numbers that are less than or equal to n, that have exactly 3 divisors 在N号之前打印每个素数 - Print every prime number before N number 打印头n个质数的复杂度 - Complexity of print first n prime number 计算小于或等于N的两个数字的对数,以使对数的数字总和为质数 - Count number of pairs of two numbers less than or equal to N such that Sum of the digits of numbers of pair is Prime 使用筛查发现小于长数的质数 - finding prime numbers less than a LONG number using sieve 编写一个 while 循环,打印所有可被 10 整除且小于给定数 n 的正数 - Write a while loop that prints all positive numbers that are divisible by 10 and less than a given number n 查找数组在小于O(n ^ 2)内重复的次数 - Find the number of times a number is repeated in an array in less than O(n^2) 程序查找数字n1的最小排列,但应大于另一个给定的数字n2; 如果可能,打印“无效” - Program to find smallest permutation of a number n1 but should be larger than another given number n2; print “Invalid” if not possible 给定数字n,则返回true就是n的所有因子都是质数。 请注意,1和数字本身不视为因素 - Given a number n, return true is all the factors of n are prime numbers. Note that 1 and the number itself are not considered as factors
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM