本人菜鸟无疑!
请教如何用bash求解:从1到100的整数中,只能被1和它本身整除的数的个数。
请列出详细的命令。
请不吝赐教!
bash应用
-
- 帖子: 151
- 注册时间: 2009-05-29 22:05
Re: bash应用
我来给楼主消灭0回复
- 枫叶饭团
- 帖子: 14683
- 注册时间: 2010-06-16 1:05
- 系统: Mac OS X
- 来自: Tencent
- 联系:
Re: bash应用
找质数?别拿来当作业哦...
[bash]
#!/bin/bash
#
# SCRIPT: prime.sh
# AUTHOR: Zhaing&Bo
# DATE: 2010-11-21
# REV: 1.1.A
#
# PLATFORM: Linularis
#
# PURPOSE: Read a number from the console, then print all the primes
# in (1~100). And at the same time calculate the sum of all
# the primes.
#
# set -n # Uncomment to check your syntax, without execution.
# # NOTE: Do not forget to put the comment back in or
# # the shell script will not execute!
# set -x # Uncomment to debug this shell script (Bash only)
#
##########################################################
########### DEFINE FILES AND VARIABLES HERE ##############
##########################################################
num=0 # 用来暂存用户输入整数
i=0 # 循环控制变量(控制is_prime函数中的循环次数)
N=0 # 待测值(is_prime函数形参)
n=0 # 主函数循环控制变量(is_prime函数实参)
flag=1 # 素数表示(其中 1 表示是,0 表示否)
sum=0 # 用来存储素数之和
##########################################################
############### DEFINE FUNCTIONS HERE ####################
##########################################################
#/**
# * 函数名称:is_prime()
# * 函数功能:测试一个数是否为素数
# */
function is_prime() {
N=$1
flag=1
for ((i=2; i<(N/2+1); i++)) {
((N%i))
if [ $? -ne 0 ]; then
flag=0; break;
fi
}
}
##########################################################
################ BEGINNING OF MAIN #######################
##########################################################
# 输出提示
read -p "Please input a postive integer:" num
#echo -e "\nYour Number is: $num.\n" # 将用户输入输出,以便用户确定
echo -e "All the prime from 1 to $num: "
for ((n=2; n<=$num; n++)) {
is_prime n
if [ $flag -eq 1 ]; then
echo -n "$n "
((sum=sum+n))
# echo -e "$n Is prime."
# else
# echo -e "$n is Not prime.\n"
fi
}
# 输出所有素数的和(1~num)
echo -e "\nThe SUM of all the primes is: $sum.\n"
# End of script 【至此结束】
[/bash]
[bash]
#!/bin/bash
#
# SCRIPT: prime.sh
# AUTHOR: Zhaing&Bo
# DATE: 2010-11-21
# REV: 1.1.A
#
# PLATFORM: Linularis
#
# PURPOSE: Read a number from the console, then print all the primes
# in (1~100). And at the same time calculate the sum of all
# the primes.
#
# set -n # Uncomment to check your syntax, without execution.
# # NOTE: Do not forget to put the comment back in or
# # the shell script will not execute!
# set -x # Uncomment to debug this shell script (Bash only)
#
##########################################################
########### DEFINE FILES AND VARIABLES HERE ##############
##########################################################
num=0 # 用来暂存用户输入整数
i=0 # 循环控制变量(控制is_prime函数中的循环次数)
N=0 # 待测值(is_prime函数形参)
n=0 # 主函数循环控制变量(is_prime函数实参)
flag=1 # 素数表示(其中 1 表示是,0 表示否)
sum=0 # 用来存储素数之和
##########################################################
############### DEFINE FUNCTIONS HERE ####################
##########################################################
#/**
# * 函数名称:is_prime()
# * 函数功能:测试一个数是否为素数
# */
function is_prime() {
N=$1
flag=1
for ((i=2; i<(N/2+1); i++)) {
((N%i))
if [ $? -ne 0 ]; then
flag=0; break;
fi
}
}
##########################################################
################ BEGINNING OF MAIN #######################
##########################################################
# 输出提示
read -p "Please input a postive integer:" num
#echo -e "\nYour Number is: $num.\n" # 将用户输入输出,以便用户确定
echo -e "All the prime from 1 to $num: "
for ((n=2; n<=$num; n++)) {
is_prime n
if [ $flag -eq 1 ]; then
echo -n "$n "
((sum=sum+n))
# echo -e "$n Is prime."
# else
# echo -e "$n is Not prime.\n"
fi
}
# 输出所有素数的和(1~num)
echo -e "\nThe SUM of all the primes is: $sum.\n"
# End of script 【至此结束】
[/bash]
- 456jian
- 帖子: 82
- 注册时间: 2011-04-18 19:06
Re: bash应用
感谢3楼的答复!