用于判断一个数是否是素数(也称为质数)
ohpm install @nutpi/isprime
import { isPrime } from "@nutpi/isprime"
@Entry
@Component
struct Index {
@State message: string = '请输入数字';
@State currentNum: number = 11;
@State res: string = '';
build() {
Column({space: 20}) {
Text(this.message)
.fontSize(20)
.fontWeight(FontWeight.Bold)
TextInput({text: this.currentNum.toString()})
.type(InputType.Number)
.onChange((value: string) => {
this.currentNum = Number(value)
})
Button('计算')
.width('80%')
.onClick(() => {
let result = isPrime(this.currentNum) ? '是素数':'不是素数';
console.info(result); // 输出: true
this.res = result
})
Text(this.res)
.fontSize(20)
.fontWeight(FontWeight.Bold)
}
.height('100%')
.width('100%')
.justifyContent(FlexAlign.Center)
}
}
https://www.nutpi.net/
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有