数学问题——找“水仙花数”

一、数学背景介绍:英国大数学家哈代曾发现一种有趣的现象:

153=1*1*1+5*5*5+3*3*3

371=3*3*3+7*7*7+1*1*1

370=3*3*3+7*7*7+0*0*0

407=4*4*4+0*0*0+7*7*7

这些是三位数,并且数值等于各位数字的三次幂之和。这种数就称为“水仙花数”。

二、要求编写代码实现找出给定范围以内的水仙花数。

package com.js.math;

import java.util.Scanner;

/**
 * 水仙花数
 * 三位数,并且数值等于各位数字的三次幂之和。这种数就称为“水仙花数”。
 * 153=1*1*1+5*5*5+3*3*3
 * @author js
 *
 */
public class NarcissusNum {
	public static void main(String[] args) {
		int boundary = 0;
		int a = 0;
		int n1 = 0;
		int n2 = 0;
		int n3 = 0;
		System.out.println("请输入查找范围...");
		Scanner scanner = new Scanner(System.in);
		if(scanner.hasNext()){
			boundary = scanner.nextInt();
			if(boundary>999)
				boundary = 999;
			for(a=100;a<boundary;a++){
				n1 = a/100;
				n2 = (a-n1*100)/10;
				n3 = (a-n1*100-n2*10);
				if(a==n1*n1*n1+n2*n2*n2+n3*n3*n3)
					System.out.println("找到一个水仙花数:"+a);
			}
		}
	}

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值