水仙花数的算法和改进

什么是水仙花数

水仙花数(Narcissistic number)也被称为超完全数字不变数(pluperfect digital invariant, PPDI)、自恋数、自幂数、阿姆斯壮数或阿姆斯特朗数(Armstrong number)。
水仙花数是指一个 3 位数,它的每个位上的数字的 3次幂之和等于它本身(例如:1^3 + 5^3+ 3^3 = 153)。

算法实现

获取三位数的计数位上面的数字,各位数字立方求和,然后和本身比较,判断是否为水仙花数。

代码实现

/**
 * 计算水仙花数
 *
 * @param sb 保存水仙花数
 */
private static void accountNarcissisticNumber1(StringBuilder sb) {
    for (int i = 100; i <= 999; i++) {
        int a = i / 100, b = i / 10 % 10, c = i % 10;
        int sum = (int) (Math.pow(a, 3) + Math.pow(b, 3) + Math.pow(c, 3));
        if (sum == i) {
            sb.append(i + ",");
        }
    }
}

改进算法

根据数据特性:奇数的立方还是奇数,偶数的立方还是偶数;奇数+奇数=偶数;奇数+偶数=奇数;

那么如果一个数字是水仙花数,那么各位数值的数据关系必然满足:

case1)三个数字中,三个奇数->个位数是奇数;

case2)三个数字中,两个奇数一个偶数,立方和必然是偶数->个位数是偶数;

case3)三个数字中,一个奇数,两个偶数,立方和必然是奇数->个位数是奇数;

/**
 * 计算水仙花数
 *
 * @param sb 保存水仙花数
 */
private static void accountNarcissisticNumber2(StringBuilder sb) {
    for (int i = 100; i <= 999; i++) {
        int a = i / 100, b = i / 10 % 10, c = i % 10;
        //a|d|c 三个奇数,则adc必须也是奇数,才可能是水仙花数
        //a|d|c 两个奇数,则adc必须也是偶数,才可能是水仙花数
        //a|d|c 一个奇数,则adc必须也是奇数,才可能是水仙花数
        //boolean threeOdds = a % 2 == 1 && b % 2 == 1 && c % 2 == 1;
        //boolean twoOdds = a % 2 == 1 && b % 2 == 1 && c % 2 == 0;
        //threeOrTwoOdds = threeOdds || twoOdds;
        boolean threeOrTwoOdds = a % 2 == 1 && b % 2 == 1;
        boolean oneOdd = c % 2 == 1 && ((a % 2 == 0 && b % 2 == 0) || (a % 2 == 1 && b % 2 == 1));
        //boolean condition = threeOrTwoOdds || oneOdd;
        boolean condition = oneOdd || (c % 2 == 0 && a % 2 == 1 && b % 2 == 1);
        if (condition) {
            int sum = (int) (Math.pow(a, 3) + Math.pow(b, 3) + Math.pow(c, 3));
            if (sum == i) {
                sb.append(i + ",");
            }
        }
    }
}

计算及对比结果

/**
 * 奇数用英文表示为:odd,odd的英式发音[ɒd],美式发音[ɑ:d] 。
 * <p>
 * 偶数用英文表示为:even,even的英式发音[ˈi:vn],美式发音[ˈivən] 。
 *
 * @param args
 */
public static void main(String[] args) {
    StringBuilder sb1 = new StringBuilder();
    StringBuilder sb2 = new StringBuilder();

    long timeBefore1 = System.nanoTime();
    accountNarcissisticNumber1(sb1);
    long timeAfter1 = System.nanoTime();

    long timeBefore2 = System.nanoTime();
    accountNarcissisticNumber2(sb2);
    long timeAfter2 = System.nanoTime();

    System.out.println(String.format("algorithm1 use nanoTime=%d", timeAfter1 - timeBefore1));
    System.out.println(String.format("algorithm1 result:%s", sb1));
    System.out.println(String.format("algorithm2 use nanoTime=%d", timeAfter2 - timeBefore2));
    System.out.println(String.format("algorithm2 result:%s", sb2));
}


//algorithm1 use nanoTime=519030
//algorithm1 result:153,370,371,407,
//algorithm2 use nanoTime=255960
//algorithm2 result:153,370,371,407,

快了大概一倍,然并卵,*^-^*!

 

附件

为方便复制,这里一起粘贴下

public class Test {

    /**
     * 奇数用英文表示为:odd,odd的英式发音[ɒd],美式发音[ɑ:d] 。
     * <p>
     * 偶数用英文表示为:even,even的英式发音[ˈi:vn],美式发音[ˈivən] 。
     *
     * @param args
     */
    public static void main(String[] args) {
        StringBuilder sb1 = new StringBuilder();
        StringBuilder sb2 = new StringBuilder();

        long timeBefore1 = System.nanoTime();
        accountNarcissisticNumber1(sb1);
        long timeAfter1 = System.nanoTime();

        long timeBefore2 = System.nanoTime();
        accountNarcissisticNumber2(sb2);
        long timeAfter2 = System.nanoTime();

        System.out.println(String.format("algorithm1 use nanoTime=%d", timeAfter1 - timeBefore1));
        System.out.println(String.format("algorithm1 result:%s", sb1));
        System.out.println(String.format("algorithm2 use nanoTime=%d", timeAfter2 - timeBefore2));
        System.out.println(String.format("algorithm2 result:%s", sb2));
    }

    /**
     * 计算水仙花数
     *
     * @param sb 保存水仙花数
     */
    private static void accountNarcissisticNumber1(StringBuilder sb) {
        for (int i = 100; i <= 999; i++) {
            int a = i / 100, b = i / 10 % 10, c = i % 10;
            int sum = (int) (Math.pow(a, 3) + Math.pow(b, 3) + Math.pow(c, 3));
            if (sum == i) {
                sb.append(i + ",");
            }
        }
    }

    /**
     * 计算水仙花数
     *
     * @param sb 保存水仙花数
     */
    private static void accountNarcissisticNumber2(StringBuilder sb) {
        for (int i = 100; i <= 999; i++) {
            int a = i / 100, b = i / 10 % 10, c = i % 10;
            //a|d|c 三个奇数,则adc必须也是奇数,才可能是水仙花数
            //a|d|c 两个奇数,则adc必须也是偶数,才可能是水仙花数
            //a|d|c 一个奇数,则adc必须也是奇数,才可能是水仙花数
            //boolean threeOdds = a % 2 == 1 && b % 2 == 1 && c % 2 == 1;
            //boolean twoOdds = a % 2 == 1 && b % 2 == 1 && c % 2 == 0;
            //threeOrTwoOdds = threeOdds || twoOdds;
            boolean threeOrTwoOdds = a % 2 == 1 && b % 2 == 1;
            boolean oneOdd = c % 2 == 1 && ((a % 2 == 0 && b % 2 == 0) || (a % 2 == 1 && b % 2 == 1));
            //boolean condition = threeOrTwoOdds || oneOdd;
            boolean condition = oneOdd || (c % 2 == 0 && a % 2 == 1 && b % 2 == 1);
            if (condition) {
                int sum = (int) (Math.pow(a, 3) + Math.pow(b, 3) + Math.pow(c, 3));
                if (sum == i) {
                    sb.append(i + ",");
                }
            }
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值