当前位置:网站首页>Kotlin算法入门计算水仙花数
Kotlin算法入门计算水仙花数
2022-08-11 08:01:00 【易庞宙】
/* 打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,其各位 数字立方和等于该数本身。例如:153是一个“水仙花数”,因为153=1的三次方 +5的三次方+3的三次方 由于100 200 300 500 400 整百数都不是水仙花可以直接排除 而且 101 为基数的也不是水仙花可以直接排除 这样排除这两个规律的直接跳过计算 */ class Narcissus { /* 开始运行时间1524629170834 153 370 371 407 结束运行时间1524629170838 耗时:4 因为有18个跳过计算立方的方法避免了计算步骤的冗余所以运行更快了 */ fun printNarcissus() { for (i in 102..998) { if (i % 100 == 0 || i % 101 == 0) continue val j = i / 100 val k = i / 10 % 10 val z = i % 10 if (i.toDouble() == Math.pow(j.toDouble(), 3.0) + Math.pow(k.toDouble(), 3.0) + Math.pow(z.toDouble(), 3.0)) println(i) } } /** * 传统算法运行结果 * * * 开始运行时间1524629049929 * 水仙花数是:153 * 水仙花数是:370 * 水仙花数是:371 * 水仙花数是:407 * 结束运行时间1524629049949 * 耗时:21 */ fun ss() { for (number in 100..999) { val gewei = number % 10 val shiwei = number / 10 % 10 val baiwei = number / 100 % 10 if (gewei * gewei * gewei + shiwei * shiwei * shiwei + baiwei * baiwei * baiwei == number) { println("水仙花数是:$number") } } } }
边栏推荐
- JUC并发编程
- The growth path of a 40W test engineer with an annual salary, which stage are you in?
- C Primer Plus(6) 中文版 第1章 初识C语言 1.7 使用C语言的7个步骤
- Square, multi-power, square root calculation in Tf
- 记录一些遇见的bug——Lombok和Mapstruct的冲突导致,A component required a bean of type ‘com.XXX.controller.converter.
- 1091 N-Defensive Number (15 points)
- The softmax function is used in TF;
- 欢迎加入sumarua网络安全交流社区
- 借问变量何处存,牧童笑称用指针,Go lang1.18入门精炼教程,由白丁入鸿儒,go lang类型指针(Pointer)的使用EP05
- 迷你图书馆系统(对象+数组)
猜你喜欢

记录一些遇见的bug——Lombok和Mapstruct的冲突导致,A component required a bean of type ‘com.XXX.controller.converter.

1051 复数乘法 (15 分)

Serverless + domain name can also build a personal blog? Really, and soon

如何通过 IDEA 数据库管理工具连接 TDengine?

1002 Write the number (20 points)

软件测试常用工具的用途及优缺点比较(详细)

【云原生】云原生在网络安全领域的应用

magical_spider远程采集方案
3.1-分类-概率生成模型

JRS303-Data Verification
随机推荐
JUC并发编程
几何EX3 功夫牛宣布停售,入门级纯电产品为何总成弃子
leetcode: 69. Square root of x
Write a resume like this, easy to get the interviewer
基于微信小程序的租房小程序
JRS303-Data Verification
magical_spider远程采集方案
The most complete documentation on Excel's implementation of grouped summation
Find the latest staff salary and the last staff salary changes
进阶-指针
Keep track of your monthly income and expenses through bookkeeping
excel 透视表 值显示内容 不显示计数
TF generates (feature, label) set through feature and label, tf.data.Dataset.from_tensor_slices
jenkins简单使用
3.2-分类-Logistic回归
pyqt5实现仪表盘
Project 1 - PM2.5 Forecast
Hibernate 的 Session 缓存相关操作
1046 划拳 (15 分)
go sqlx 包