import scala.math.sqrt
import scala.util.control.Breaks.{break, breakable}
object Test {
def main(args: Array[String]): Unit = {
println("20的累加和="+sum(20))//20的累加
formula9_9()//99乘法口诀
sushuNum(101,200);//求左闭右开区间素数
}
//20累加
def sum(n:Int):Int= {
if (n >= 1)
sum(n - 1)+n
else
n
}
//99乘法口诀
def formula9_9():Unit={
var result=0
for (n1<- 1 to 9 ){
for (n2<- 1 to 9 ){
result=n1 * n2
println(n1+"*"+n2+"="+result)
}
}
}
def sushuNum(x1:Int,x2:Int):Unit={
var index = 0
var count=0
for (s<- x1 until x2){
var flag=false
breakable{
index=2
while(index<=sqrt(s)){
if(s % index==0){
//不是素数
flag=true
break
}
index+=1
}
}
if(!flag) count+=1
}
println("个数="+count)
}
}
网友评论