zoukankan      html  css  js  c++  java
  • 第六章第二十六题(回文素数)(Palindromic prime)

    **6.26(回文素数)回文素数是指一个数同时为素数和回文数。例如:131是一个素数,同时也是一个回文素数。数学313和757也是如此。编写程序,显示前100个回文素数。每行显示10个数,数字中间用一个空格隔开。如下所示:

    2 3 5 7 11 101 131 151 181 191

    313 353 373 383 727 757 787 797 919 929

    **6.26(Palindromic prime) A palindromic prime is a prime number and also palindromic. For example, 131 is a prime and also a palindromic prime, as are 313 and 757. Write a program that displays the first 120 palindromic prime numbers. Display 10 numbers per line, separated by exactly one space, as follows:

    2 3 5 7 11 101 131 151 181 191

    313 353 373 383 727 757 787 797 919 929

    下面是参考答案代码:

    // https://cn.fankuiba.com
    public class Ans6_26_page202 {
        public static void main(String[] args) {
            isPrime(100,10);
        }
        public static void isPrime(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) {
                // 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--;
                }
    
                // Print the prime number and increase the count
                if (isPrime && isPalindrome) {
                    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版)更多内容

  • 相关阅读:
    [LuoguP2161] 会场预约
    [LuoguP1198] 最大数
    [LuoguP1484] 种树
    [LuoguP1801] 黑匣子
    [LuoguP1196]银河英雄传说
    [LuoguP1345] 奶牛的电信Telecowmunication
    [LuoguP1119]灾后重建
    【笔记】一元函数微分学
    【复习】Listening and Reading Comprehension
    【笔记】一元函数的不定积分
  • 原文地址:https://www.cnblogs.com/in2013/p/12905156.html
Copyright © 2011-2022 走看看