用于判断一个数是否是素数(也称为质数)
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/