当前位置:网站首页>Getting Started with Kotlin Algorithm to Calculate the Number of Daffodils
Getting Started with Kotlin Algorithm to Calculate the Number of Daffodils
2022-08-11 08:13:00 【Yi Pangzhou】
/* 打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,其各位 数字立方和等于该数本身.例如: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") } } } }
边栏推荐
猜你喜欢
Write a resume like this, easy to get the interviewer
囍楽cloud task source code
笔试题大疆08.07
【TA-霜狼_may-《百人计划》】图形3.7.2 command buffer简
JRS303-Data Verification
零基础SQL教程: 基础查询 05
Use tf.argmax in Tensorflow to return the index of the maximum value of the tensor along the specified dimension
Machine Learning Summary (2)
2022-08-10:为了给刷题的同学一些奖励,力扣团队引入了一个弹簧游戏机, 游戏机由 N 个特殊弹簧排成一排,编号为 0 到 N-1, 初始有一个小球在编号 0 的弹簧处。若小球在编号为 i 的弹
软件测试常用工具的用途及优缺点比较(详细)
随机推荐
Active users of mobile banking grew rapidly in June, hitting a half-year high
初级软件测试工程师笔试试题,你知道答案吗?
One-hot in TF
一根网线两台电脑传输文件
优炫数据库支持多列分区吗?
第一次因没有找到iframe元素而怀疑selenium4是不是有bug?
2022 China Soft Drink Market Insights
Four operations in TF
关于#sql#的问题:怎么将下面的数据按逗号分隔成多行,以列的形式展示出来
C Primer Plus(6) 中文版 第1章 初识C语言 1.6 语言标准
【Day_13 0509】▲跳石板
Write a resume like this, easy to get the interviewer
3.1-Classification-probabilistic generative model
Nuget can't find the package problem
Find the latest staff salary and the last staff salary changes
Test cases are hard?Just have a hand
RestTemplate工具类
如何通过开源数据库管理工具 DBeaver 连接 TDengine
Kotlin算法入门计算素数以及优化
1.2 - error sources