zoukankan      html  css  js  c++  java
  • 第六章第二十七题(反素数)(Emirp)

    **6.27(反素数)反素数(反转拼写的素数)是指一个非回文素数,将其反转之后也是一个素数。例如:17是一个素数,而31也是一个素数,所以17和71是反素数。编写程序,显示前100个反素数。每行显示10个,并且数字间用空格隔开,如下所示:

    13 17 31 37 71 73 79 97 107 113

    149 157 167 179 199 311 337 347 359 389

    **6.27(Emirp)An emirp (prime spelled backward) is a nonpalindromic prime number whose reversal is also a prime. For example, 17 is a prime and 71 is a prime, so 17 and 71 are emirps. Write a program that displays the first 120 emirps. Display 10 numbers per line, separated by exactly one space, as follows:

    13 17 31 37 71 73 79 97 107 113

    149 157 167 179 199 311 337 347 359 389

    下面是参考答案代码:

    // https://cn.fankuiba.com
    public class Ans6_27_page202 {
        public static void main(String[] args) {
            ReverseePrime(100,10);
        }
        public static void ReverseePrime(int n, int line) {
            int count = 0; // Count the number of prime numbers
            int number = 2; // A number to be tested for primeness
    
            System.out.println("The first "+n+" numbers are 
    ");
    
            // Repeatedly find prime numbers
            while (count < n) {
                String strNumber = number+"";
                // Assume the number is prime
                boolean isPrime = true; // Is the current number prime?
    
                // Test if number is prime
                for (int divisor = 2; divisor <= number / 2; divisor++) {
                    if (number % divisor == 0) { // If true, number is not prime
                        isPrime = false; // Set isPrime to false
                        break; // Exit the for loop
                    }
                }
    
                // Test if number is palindrome
                boolean isPalindrome = true;
                //String strNumber = number+"";
                int low = 0;
                int high = strNumber.length() - 1;
                while (low < high) {
                    if (strNumber.charAt(low) != strNumber.charAt(high)) {
                        isPalindrome = false;
                        break;
                    }
                    low++;
                    high--;
                }
    
                // reverseNumber
                int tempNumber = number;
                String strReverseNumber = "";
                while (tempNumber != 0) {
                    strReverseNumber += tempNumber % 10;
                    tempNumber /=10;
                }
                int reverseNumber = Integer.parseInt(strReverseNumber);
                // ReverseePrime
                boolean isReverseePrime = true;
                for (int divisor = 2; divisor <= reverseNumber / 2; divisor++) {
                    if (reverseNumber % divisor == 0) {
                        isReverseePrime = false;
                        break;
                    }
                }
    
                // Print the prime number and increase the count
                if (isPrime && !isPalindrome && isReverseePrime) {
                    count++; // Increase the count
    
                    if (count % line == 0) {
                        // Print the number and advance to the new line
                        System.out.println(number);
                    }
                    else
                        System.out.print(number + " ");
                }
    
                // Check if the next number is prime
                number++;
            }
        }
    }
    

    适用Java语言程序设计与数据结构(基础篇)(原书第11版)Java语言程序设计(基础篇)(原书第10/11版)更多内容

  • 相关阅读:
    支持向量机SVM知识点概括
    决策树知识点概括
    HDU 3081 Marriage Match II
    HDU 3572 Task Schedule
    HDU 4888 Redraw Beautiful Drawings
    Poj 2728 Desert King
    HDU 3926 Hand in Hand
    HDU 1598 find the most comfortable road
    HDU 4393 Throw nails
    POJ 1486 Sorting Slides
  • 原文地址:https://www.cnblogs.com/in2013/p/12911905.html
Copyright © 2011-2022 走看看