简体   繁体   English

打印 100 到 1000 之间的素数,其数字总和等于 19

[英]Printing prime numbers between 100 to 1000 whose digit sum equals to 19

I have this problem.我有这个问题。 I have written the code for the same but I am unable to find the error where I am doing mistake.我已经编写了相同的代码,但我无法找到我做错的地方。 It is printing 100 as output nothing else.它打印 100 为 output 没有别的。

package practicepkg;

import java.util.Scanner;

public class PrimeNumber {

    public static void main(String[] args) {
        System.out.println("Enter the number to check");
        Scanner in = new Scanner(System.in);
        //taking input for 1000
        int number =  in.nextInt();
        int count=0;
        int k,num=100,sum=0;
        //running loop from 100 to till user input(1000)
        for(k=100;k<=number;k++)
        {
            //loop for checking prime number
             for(int i=2;i<(int)(Math.sqrt(number));i++)
                    {
                        if(k%i==0)
                        {
                            count+=1;
                            
                        }
                        //here only taking prime numbers
                        if(count==0)
                        {
                            num=k;
                            //using while loop to calculate sum
                            while(num!=0)
                            {
                                sum=sum+num%10;
                                num=num/10;
                            }
                            //comparing and printing the actual prime number value for k
                            if(sum==19)
                            System.out.println(k);
                        }
                        //resetting the count value to zero for next iteration
                        count=0;    
                                            
                     }
                                     
        }
        
    }
}  

Using your basic brute force approach, the sum should be reset to 0 for each prime number and we need to calculate the sum after we have completed checking if the number is prime or not.使用基本的蛮力方法,每个素数的总和应重置为 0,我们需要在完成检查数字是否为素数后计算总和。

public static void main(String[] args) {
    System.out.println("Enter the number to check");
    Scanner in = new Scanner(System.in);
    //taking input for 1000
    int number =  in.nextInt();
    int count=0;
    int k,num=100,sum=0;
    //running loop from 100 to till user input(1000)
    for(k=100;k<=number;k++)
    {
        //loop for checking prime number
         for(int i=2;i<(int)(Math.sqrt(number));i++)
         {
             if(k%i==0)
             {
                 count+=1;
             }                   
         }
         //This should be outside the loop that checks the number for prime
         if(count==0)
         {
             //Sum should be reset for each new prime number
             sum=0;
             num=k;
             //using while loop to calculate sum
             while(num!=0)
             {
                 sum=sum+num%10;
                 num=num/10;
             }
             //comparing and printing the actual prime number value for k
             if(sum==19)
                 System.out.println(k);
         }
         //resetting the count value to zero for next iteration
         count=0;            
    }
    
}

Edit: Optimal solution to the above problem.编辑:上述问题的最佳解决方案。 Thanks to @D George to point it out感谢@D George 指出

public static void main(String[] args) {
    System.out.println("Enter the number to check");
    Scanner in = new Scanner(System.in);
    //taking input for 1000
    int number =  in.nextInt();
    boolean flag = false;
        int k,num=100,sum=0;
        //running loop from 100 to till user input(1000)
        for(k=100;k<=number;k++)
        {
            //resetting the flag value to false for next iteration
             flag = false;
            //loop for checking prime number
             for(int i=2;i<(int)(Math.sqrt(number));i++)
             {
                 if(k%i==0)
                 {
                     flag=true;
                     break;
                 }                   
             }
             //This should be outside the loop that checks the number for prime
             if(!flag)
             {
                 //Sum should be reset for each new prime number
                 sum = 0;
                 num = k;
                 //using while loop to calculate sum
                 while(num!=0)
                 {
                     sum += num%10;
                     num /= 10;
                 }
                 //comparing and printing the actual prime number value for k
                 if(sum==19) {
                     System.out.println(k);
                 }
             }

        }
 }

You are looking at numbers between 100 and 1000. In that range, all numbers ending in an even digit, or ending with 5, are not prime so you don't even need to bother checking them.您正在查看 100 到 1000 之间的数字。在该范围内,所有以偶数结尾或以 5 结尾的数字都不是质数,因此您甚至无需费心检查它们。 Prime numbers can only end in 1, 3, 7 or 9 within that range.在该范围内,质数只能以 1、3、7 或 9 结尾。

That gives you pseudocode something like:这为您提供了伪代码,例如:

for (base <- 100; base < 1000; step 10)
  test(base + 1)
  test(base + 3)
  test(base + 7)
  test(base + 9)
endfor

This can be further refined by checking the digit sum of base before prime testing.这可以通过在素数测试之前检查base的数字和来进一步细化。 You only need bother to check further where the digit sum of base is one of (19 - 1), (19 - 3), (19 - 7) or (19 - 9).您只需要进一步检查base之和是 (19 - 1)、(19 - 3)、(19 - 7) 或 (19 - 9) 之一的位置。 That means you can step through the range 100 to 1000 only looking at every tenth number, calculating its digit sum and checking either none or just one of the potential primes in the ten numbers following.这意味着您可以遍历 100 到 1000 的范围,只查看每十个数字,计算其数字总和,并检查后面十个数字中的一个潜在质数或一个潜在质数。

You could shave off some more time by noticing that the smallest three digit number with a digit sum of 19 is 199, so you could start base at 190 instead of at 100.您可以通过注意数字和为 19 的最小三位数是 199 来节省更多时间,因此您可以从base而不是 100 开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM