如何将输入数字(素数)的两位数之和显示出来。

huangapple go评论61阅读模式
英文:

How possible to display the 2 digit sum of the inputted number (prime numbers)

问题

import java.util.Scanner;

class Prime {

    boolean prime(int a) {
        int count = 0;
        for (int x = 1; x <= a; x++) {
            if (a % x == 0) {
                count++;
            }
        }

        switch (count) {
            case 2:
                return true;
            default:
                return false;
        }
    }

    public static void main(String args[]) {

        Prime twin = new Prime();
        Scanner sc = new Scanner(System.in);

        int higherThan2 = 3;
        System.out.print("Enter the number: ");
        int number = sc.nextInt();

        if (higherThan2 >= number) {
            System.out.println("Must be higher than 3!");
        } else {
            System.out.println("\nThe twin prime numbers of " + number + " are: ");
            for (int i = higherThan2; i <= (number - 2); i++) {
                if (twin.prime(i) == true && twin.prime(i + 2) == true) {
                    System.out.print(i + " / " + (i + 2) + "\n");
                }
            }
        }
    }
}
英文:

It will only accept a non-negative even number (higher than 2) and extract the 2 prime numbers. Here is the sample output:

Enter the number: 20

The twin prime numbers of 20 are: 17 and 3 / 13 and 7

(17 + 3 = 20 and 13 + 7 = 20 also) But 17,3,13, and 7 is a prime number. I don't know how to do it, please, thankyou!

But here is my output, my output is just to print all twin prime between 3-20 (that is inputted number)
run:

Enter the number: 20

The twin prime numbers of 20 are:
3 / 5
5 / 7
11 / 13
17 / 19

I don't know how to do the specific problem.

Here is my code btw:

import java.util.Scanner;
class Prime {

    boolean prime(int a) {
        int count = 0;
        for (int x = 1; x &lt;= a; x++) {
            if (a % x == 0) {
                count++;
            }
        }
        
        switch (count) {
            case 2:
                return true;
            default:
                return false;
        }
    }

    public static void main(String args[]) {
        
        Prime twin = new Prime();
        Scanner sc = new Scanner(System.in);

        int higherThan2 = 3;
        System.out.print(&quot;Enter the number: &quot;);
        int number = sc.nextInt();

        if (higherThan2 &gt;= number) {
            System.out.println(&quot;Must be higher than 3!&quot;);
        } else {
            System.out.println(&quot;\nThe twin prime numbers of &quot; + number + &quot; are: &quot;);
            for (int i = higherThan2; i &lt;= (number - 2); i++) {
                if (twin.prime(i) == true &amp;&amp; twin.prime(i + 2) == true) {
                    System.out.print(i + &quot; / &quot; + (i + 2) + &quot;\n&quot;);
                }
            }
        }
    }
}

答案1

得分: 1

你可以试试这个。它可以工作,但我不认为这是最好的。

 System.out.println("与" + number + "相邻的孪生质数是:");
for (int i = higherThan2; i < number; i++)
for (int j = number; j > higherThan2; j--)
if (twin.prime(i) && twin.prime(j) && i + j == number)
System.out.print(i + " / " + j + "\n");
英文:

You can try this. It works, but I don't think it's the best

 System.out.println(&quot;\nThe twin prime numbers of &quot; + number + &quot; are: &quot;);
for(int i = higherThan2; i &lt; number; i++)
for(int j = number; j &gt; higherThan2; j--)
if(twin.prime(i) &amp;&amp; twin.prime(j) &amp;&amp; i + j == number)
System.out.print(i + &quot; / &quot; + j + &quot;\n&quot;);

huangapple
  • 本文由 发表于 2020年10月1日 19:06:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/64154098.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定