swift编程验证100以内的哥德巴赫猜想。 将数字分解为两个数之和判断是否均为质数 每5个表达式换一行输出
答案:1 悬赏:20 手机版
解决时间 2021-11-15 10:57
- 提问者网友:听门外雪花风
- 2021-11-14 13:42
swift编程验证100以内的哥德巴赫猜想。 将数字分解为两个数之和判断是否均为质数 每5个表达式换一行输出
最佳答案
- 五星知识达人网友:孤独入客枕
- 2021-11-14 14:46
// 哥德巴赫猜想 100以内验证程序
extension Int {
public var 是质数: Bool {
get {
switch self {
case 0: return false
case 1: return true
case 2: return true
default:
for i in 2 ... self / 2 + 1 {
guard self % i != 0 else {
return false
}//end guard
}//next
}//next
return true
}//end get
}//end var
}//end extension
var 质数表 = [Int]()
for 某整数 in 1 ... 99 {
if 某整数.是质数 {
质数表.append(某整数)
}//如果
}//下一个
var 哥德巴赫表 = [String]()
for 某整数 in 2 ... 100 {
var 找到了 = false
for 甲 in 质数表 {
for 乙 in 质数表 {
let 和 = 甲 + 乙
if 某整数 == 和 {
哥德巴赫表.append("(和) = (甲) + (乙) ")
找到了 = true
break
}//如果
}//下一个
if 找到了 {
break
}//如果
}//下一个
}//下一个
var 计数器 = 0
for 表达式 in 哥德巴赫表 {
计数器 += 1
print(表达式, terminator: 计数器 % 5 == 0 ? "
": "")
}//下一个运行后应该输出:
2 = 1 + 1 3 = 1 + 2 4 = 1 + 3 5 = 2 + 3 6 = 1 + 5
7 = 2 + 5 8 = 1 + 7 9 = 2 + 7 10 = 3 + 7 12 = 1 + 11
13 = 2 + 11 14 = 1 + 13 15 = 2 + 13 16 = 3 + 13 18 = 1 + 17
19 = 2 + 17 20 = 1 + 19 21 = 2 + 19 22 = 3 + 19 24 = 1 + 23
25 = 2 + 23 26 = 3 + 23 28 = 5 + 23 30 = 1 + 29 31 = 2 + 29
32 = 1 + 31 33 = 2 + 31 34 = 3 + 31 36 = 5 + 31 38 = 1 + 37
39 = 2 + 37 40 = 3 + 37 42 = 1 + 41 43 = 2 + 41 44 = 1 + 43
45 = 2 + 43 46 = 3 + 43 48 = 1 + 47 49 = 2 + 47 50 = 3 + 47
52 = 5 + 47 54 = 1 + 53 55 = 2 + 53 56 = 3 + 53 58 = 5 + 53
60 = 1 + 59 61 = 2 + 59 62 = 1 + 61 63 = 2 + 61 64 = 3 + 61
66 = 5 + 61 68 = 1 + 67 69 = 2 + 67 70 = 3 + 67 72 = 1 + 71
73 = 2 + 71 74 = 1 + 73 75 = 2 + 73 76 = 3 + 73 78 = 5 + 73
80 = 1 + 79 81 = 2 + 79 82 = 3 + 79 84 = 1 + 83 85 = 2 + 83
86 = 3 + 83 88 = 5 + 83 90 = 1 + 89 91 = 2 + 89 92 = 3 + 89
94 = 5 + 89 96 = 7 + 89 98 = 1 + 97 99 = 2 + 97 100 = 3 + 97
extension Int {
public var 是质数: Bool {
get {
switch self {
case 0: return false
case 1: return true
case 2: return true
default:
for i in 2 ... self / 2 + 1 {
guard self % i != 0 else {
return false
}//end guard
}//next
}//next
return true
}//end get
}//end var
}//end extension
var 质数表 = [Int]()
for 某整数 in 1 ... 99 {
if 某整数.是质数 {
质数表.append(某整数)
}//如果
}//下一个
var 哥德巴赫表 = [String]()
for 某整数 in 2 ... 100 {
var 找到了 = false
for 甲 in 质数表 {
for 乙 in 质数表 {
let 和 = 甲 + 乙
if 某整数 == 和 {
哥德巴赫表.append("(和) = (甲) + (乙) ")
找到了 = true
break
}//如果
}//下一个
if 找到了 {
break
}//如果
}//下一个
}//下一个
var 计数器 = 0
for 表达式 in 哥德巴赫表 {
计数器 += 1
print(表达式, terminator: 计数器 % 5 == 0 ? "
": "")
}//下一个运行后应该输出:
2 = 1 + 1 3 = 1 + 2 4 = 1 + 3 5 = 2 + 3 6 = 1 + 5
7 = 2 + 5 8 = 1 + 7 9 = 2 + 7 10 = 3 + 7 12 = 1 + 11
13 = 2 + 11 14 = 1 + 13 15 = 2 + 13 16 = 3 + 13 18 = 1 + 17
19 = 2 + 17 20 = 1 + 19 21 = 2 + 19 22 = 3 + 19 24 = 1 + 23
25 = 2 + 23 26 = 3 + 23 28 = 5 + 23 30 = 1 + 29 31 = 2 + 29
32 = 1 + 31 33 = 2 + 31 34 = 3 + 31 36 = 5 + 31 38 = 1 + 37
39 = 2 + 37 40 = 3 + 37 42 = 1 + 41 43 = 2 + 41 44 = 1 + 43
45 = 2 + 43 46 = 3 + 43 48 = 1 + 47 49 = 2 + 47 50 = 3 + 47
52 = 5 + 47 54 = 1 + 53 55 = 2 + 53 56 = 3 + 53 58 = 5 + 53
60 = 1 + 59 61 = 2 + 59 62 = 1 + 61 63 = 2 + 61 64 = 3 + 61
66 = 5 + 61 68 = 1 + 67 69 = 2 + 67 70 = 3 + 67 72 = 1 + 71
73 = 2 + 71 74 = 1 + 73 75 = 2 + 73 76 = 3 + 73 78 = 5 + 73
80 = 1 + 79 81 = 2 + 79 82 = 3 + 79 84 = 1 + 83 85 = 2 + 83
86 = 3 + 83 88 = 5 + 83 90 = 1 + 89 91 = 2 + 89 92 = 3 + 89
94 = 5 + 89 96 = 7 + 89 98 = 1 + 97 99 = 2 + 97 100 = 3 + 97
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯