• (7)如何得到所有的 "水仙花数" ?


    本程序转载自:如何得到所有的水仙花数

    感谢Android_iPhone日知己所无),preferme冰思雨)等人;


    package test;
    
    import java.math.BigInteger;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.concurrent.BlockingQueue;
    import java.util.concurrent.LinkedBlockingQueue;
    
    /**
     * 找出所有的水仙花数
     * 判断一个数是否为水仙花数:一个N位整数,其各位数字的N次方的和等于该数本身
     *
     * @author preferme (冰思雨)
     *
     */
    public class ShuiXianHuaTree {
    
    	public static final SimpleDateFormat SDF = new SimpleDateFormat(
    			"yyyy-MM--dd HH:mm:ss.SSS");
    	public static final BigInteger EndPoint = new BigInteger(
    			"115132219018763992565095597973971522402");
    	private static final BigInteger Poison = new BigInteger("-1");
    
    	private static class CompareThread extends Thread {
    		private BlockingQueue<BigInteger> numbers;
    
    		public CompareThread(BlockingQueue<BigInteger> queue) {
    			numbers = queue;
    		}
    
    		public void run() {
    			BigInteger number = BigInteger.ZERO;
    			try {
    				while ((number = numbers.take()) != Poison) {
    					if (isNarcissisticNumber(number)) {
    						System.out.println(SDF.format(new Date()) + "	"
    								+ number);
    					}
    				}
    			} catch (InterruptedException e) {
    				e.printStackTrace();
    			}
    		}
    	}
    
    	private static final BigInteger[][] Powers = new BigInteger[40][10];
    	static {
    		for (int i = 0; i < 40; i++) {
    			for (int j = 0; j < 10; j++) {
    				Powers[i][j] = BigInteger.valueOf(j).pow(i);
    			}
    		}
    	}
    
    	public static boolean isNarcissisticNumber(final BigInteger number) {
    		BigInteger sum = BigInteger.ZERO; // 各位数字的N次方的和
    		char[] digitArray = number.toString(10).toCharArray(); // 各位数字的数组
    		for (char digit : digitArray) {
    			sum = sum.add(Powers[digitArray.length][digit - '0']);
    		}
    		return sum.compareTo(number) == 0;
    	}
    
    	public static void main(String[] args) throws InterruptedException {
    		BlockingQueue<BigInteger> queue = new LinkedBlockingQueue<BigInteger>(
    				1000);
    		CompareThread[] pool = new CompareThread[Runtime.getRuntime()
    				.availableProcessors()];
    		for (int i = 0; i < pool.length; i++) {
    			pool[i] = new CompareThread(queue);
    			pool[i].setPriority(Thread.MIN_PRIORITY);
    			pool[i].start();
    		}
    		System.out.println("水仙花数列表");
    		for (BigInteger number = BigInteger.ZERO; number.compareTo(EndPoint) <= 0; number = number
    				.add(BigInteger.ONE)) {
    			queue.put(number);
    		}
    	}
    }
    


  • 相关阅读:
    测试
    unittest发送测试报告邮件
    unittest生成测试报告
    Navicat连接Oracle详细教程
    Windows 2012 安装 Oracle 11g 报错:[INS-13001]环境不满足最低要求。
    Windows server 2012安装oracle11g(32/64位)步骤
    ElasticSearch和solr的差别
    HashMap和Hashtable的区别
    final关键字所修饰的类有什么特点
    springboot测试的时候插入数据: error performing isolated work; SQL [n/a]; nested exception is org.hibernate...
  • 原文地址:https://www.cnblogs.com/xiaozhang2014/p/5297283.html
Copyright © 2020-2023  润新知