# golang判断素数，水仙花数和求阶乘

Odven · · 134 次点击 · · 开始浏览

### 1) 判断素数

``````// main.go
package main

import (
"fmt"
"math"
)

func isPrime(a int) bool {
if a == 1 {
return false
}
for i := 2; i <= int(math.Sqrt(float64(a))); i++ {
if a%i == 0 {
return false
}
}
return true
}

func main() {
var n int
var m int
fmt.Scanf("%d %d", &n, &m)
for i := n; i < m; i++ {
if isPrime(i) == true {
fmt.Printf("%d ---> 是素数\n", i)
}
}
}

// main.py
#!/usr/bin/env python
# _*_ coding:utf-8 _*_

def isPrime(a):
if a == 1:
return False
for i in range(2, a):
if a % i == 0:
return False

return True

def main():
for i in range(1, 10):
if isPrime(i) == True:
print("%s ---> 是素数" % i)

if __name__ == '__main__':
main()
``````

### 2) 判断水仙花数

``````// main.go
package main

import (
"fmt"
"strconv"
)

func isSxh(a int) int {
var sum int
s := strconv.Itoa(a)  // 把数字转换为字符
// fmt.Println(s)
// fmt.Println(len(s))
for i := 0; i < len(s); i++ {
j := fmt.Sprintf("%c", s[i])  // s[i]是相应字符对应的ascii码  ，使用fmt.Sprintf("%c", s[i])转换ascii码对应的真正的字符
// fmt.Println(j)
k, _ := strconv.Atoi(j)  // 把字符转换为数字
sum += k * k * k
}
// fmt.Println(strings.Repeat("#", 30))
return sum
}

func main() {
for i := 1; i < 2000; i++ {
// result := isSxh(i)
// fmt.Println(result)
if isSxh(i) == i {
fmt.Printf("%d ---> 是水仙花数\n", i)
}
}
}

// main.py
#!/usr/bin/env python
# _*_ coding:utf-8 _*_

def isSxh(a):
s = "%s" % a
sum = 0
for i in range(len(s)):
sum += int(s[i]) ** 3
return sum

def main():
for i in range(10, 2000):
if isSxh(i) == i:
print("%s ---> 是水仙花数" % i)

if __name__ == '__main__':
main()
``````

### 3) 求阶乘

``````// mian.go
package main

import "fmt"

func jiecheng(a int) {
var sum = 1
for i := 1; i <= a; i++ {
sum = sum * i
}
fmt.Printf("%d 的阶乘是 %d\n", a, sum)
}

func main() {
for i := 1; i < 10; i++ {
jiecheng(i)
}
}

// main.py
#!/usr/bin/env python
# _*_ coding:utf-8 _*_

def jiecheng(a):
if a <= 0:
print("不允许小于等于0")
return
sum = 1
for i in range(1, a + 1):
sum = sum * i
print("%s 的阶乘是 %s" % (a, sum))

def main():
for i in range(1, 10):
jiecheng(i)

if __name__ == '__main__':
main()
``````

0 回复

• 请尽量让自己的回复能够对别人有帮助
• 支持 Markdown 格式, **粗体**、~~删除线~~、``单行代码``
• 支持 @ 本站用户；支持表情（输入 : 提示），见 Emoji cheat sheet
• 图片支持拖拽、截图粘贴等方式上传

### 1) 判断素数

``````// main.go
package main

import (
"fmt"
"math"
)

func isPrime(a int) bool {
if a == 1 {
return false
}
for i := 2; i <= int(math.Sqrt(float64(a))); i++ {
if a%i == 0 {
return false
}
}
return true
}

func main() {
var n int
var m int
fmt.Scanf("%d %d", &n, &m)
for i := n; i < m; i++ {
if isPrime(i) == true {
fmt.Printf("%d ---> 是素数\n", i)
}
}
}

// main.py
#!/usr/bin/env python
# _*_ coding:utf-8 _*_

def isPrime(a):
if a == 1:
return False
for i in range(2, a):
if a % i == 0:
return False

return True

def main():
for i in range(1, 10):
if isPrime(i) == True:
print("%s ---> 是素数" % i)

if __name__ == '__main__':
main()
``````

### 2) 判断水仙花数

``````// main.go
package main

import (
"fmt"
"strconv"
)

func isSxh(a int) int {
var sum int
s := strconv.Itoa(a)  // 把数字转换为字符
// fmt.Println(s)
// fmt.Println(len(s))
for i := 0; i < len(s); i++ {
j := fmt.Sprintf("%c", s[i])  // s[i]是相应字符对应的ascii码  ，使用fmt.Sprintf("%c", s[i])转换ascii码对应的真正的字符
// fmt.Println(j)
k, _ := strconv.Atoi(j)  // 把字符转换为数字
sum += k * k * k
}
// fmt.Println(strings.Repeat("#", 30))
return sum
}

func main() {
for i := 1; i < 2000; i++ {
// result := isSxh(i)
// fmt.Println(result)
if isSxh(i) == i {
fmt.Printf("%d ---> 是水仙花数\n", i)
}
}
}

// main.py
#!/usr/bin/env python
# _*_ coding:utf-8 _*_

def isSxh(a):
s = "%s" % a
sum = 0
for i in range(len(s)):
sum += int(s[i]) ** 3
return sum

def main():
for i in range(10, 2000):
if isSxh(i) == i:
print("%s ---> 是水仙花数" % i)

if __name__ == '__main__':
main()
``````

### 3) 求阶乘

``````// mian.go
package main

import "fmt"

func jiecheng(a int) {
var sum = 1
for i := 1; i <= a; i++ {
sum = sum * i
}
fmt.Printf("%d 的阶乘是 %d\n", a, sum)
}

func main() {
for i := 1; i < 10; i++ {
jiecheng(i)
}
}

// main.py
#!/usr/bin/env python
# _*_ coding:utf-8 _*_

def jiecheng(a):
if a <= 0:
print("不允许小于等于0")
return
sum = 1
for i in range(1, a + 1):
sum = sum * i
print("%s 的阶乘是 %s" % (a, sum))

def main():
for i in range(1, 10):
jiecheng(i)

if __name__ == '__main__':
main()
``````