VB循环程序例题附答案
1.素数
1. [100,999]范围内同时满足以下两个条件的十进制数. ⑴其个位数字与十位数字之和除以10所得的余数是百位数字;⑵该数是素数; 求有多少个这样的数? 15 n = 0
For i = 100 To 999 a = i \\ 100
b = i \\ 10 Mod 10 c = i Mod 10
If (c + b) Mod 10 = a Then x = 0
For j = 2 To i - 1
If i Mod j = 0 Then x = 1: Exit For Next j
If x = 0 Then n = n + 1 End If Next i Print n
2. [300,800]范围内同时满足以下两个条件的十进制数. ⑴其个位数字与十位数字之和除以10所得的余数是百位数字 ;⑵该数是素数;求满足上述条件的最大的三位十进制数。 761 For i = 300 To 800 a = i \\ 100
b = i \\ 10 Mod 10 c = i Mod 10
If (c + b) Mod 10 = a Then x = 0
For j = 2 To i - 1
If i Mod j = 0 Then x = 1: Exit For Next j
If x = 0 Then s = i End If Next i Print s
3. 除1和它本身外,不能被其它整数整除的正整数称为素数(注:1不是素数,2是素数)。若两素数之差为2 ,则称两素数为双胞胎数,问[31,601]之间有多少对双胞胎数。 22 n = 0
For i = 31 To 601 - 2 x = 0
For j = 2 To Sqr(i)
第 1 页
If i Mod j = 0 Then x = 1: Exit For Next j
If x = 0 Then y = 0
For j = 2 To Sqr(i + 2)
If (i + 2) Mod j = 0 Then y = 1: Exit For Next j
If y = 0 Then n = n + 1 End If Next i Print n
4. 数学家哥德巴赫曾猜测:任何大于6的偶数都可以分解成两个素数(素数对)的和。但有些偶数可以分解成多种素数对的和,如: 10=3+7,10=5+5,即10可以分解成两种不同的素数对。试求6744可以分解成多少种不同的素数对(注: A+B与B+A认为是相同素数对) 144 n = 0
For i = 2 To 6744 / 2 x = 0
For j = 2 To Sqr(i)
If i Mod j = 0 Then x = 1: Exit For Next j
If x = 0 Then y = 0
For j = 2 To Sqr(6744 - i)
If (6744 - i) Mod j = 0 Then y = 1: Exit For Next j
If y = 0 Then n = n + 1 End If Next i Print n
5. 两个素数之差为2,则称这两个素数为双胞胎数。求出[200,1000]之间的最大一对双胞胎数的和。 1764 For i = 200 To 1000 - 2 x = 0
For j = 2 To Sqr(i)
If i Mod j = 0 Then x = 1: Exit For Next j
If x = 0 Then y = 0
For j = 2 To Sqr(i + 2)
If (i + 2) Mod j = 0 Then y = 1: Exit For Next j
If y = 0 Then s = i + i + 2 End If Next i
第 2 页
Print s
6. 一个素数(设为p)依次从最高位去掉一位,二位,三位,??,若得到的各数仍都是素数(注:除1和它本身外,不能被其它整数整除的正整数称为素数,1不是素数,2是素数),且数p的各位数字均不为零,则称该数p为逆向超级素数。例如,617,17,7都是素数,因此617是逆向超级素数,尽管503,03,3都是素数,但它不是逆向超级素数,因为它包含有零。试求[100,999]之内的所有逆向超级素数的个数。 39 n = 0
For i = 100 To 999 a = i \\ 10 Mod 10 b = i Mod 10
If a <> 0 And b <> 0 And b <> 1 Then x = 0
For j = 2 To Sqr(i)
If i Mod j = 0 Then x = 1: Exit For Next j
If x = 0 Then y = 0
c = i Mod 100
For j = 2 To Sqr(c)
If c Mod j = 0 Then y = 1: Exit For Next j
If y = 0 Then z = 0
For j = 2 To Sqr(b)
If b Mod j = 0 Then z = 1: Exit For Next j
If z = 0 Then n = n + 1 End If End If End If Next i Print n
7. 德国数学家哥德巴赫曾猜测:任何大于6的偶数都可以分解成两个素数(素数对)的和。但有些偶数可以分解成多种素数对的和,如: 10=3+7,10=5+5,即10可以分解成两种不同的素数对。试求1234可以分解成多少种不同的素数对(注: A+B与B+A认为是相同素数对) 25 n = 0
For i = 2 To 1234 / 2 x = 0
For j = 2 To Sqr(i)
If i Mod j = 0 Then x = 1: Exit For Next j
If x = 0 Then y = 0
For j = 2 To Sqr(1234 - i)
第 3 页
If (1234 - i) Mod j = 0 Then y = 1: Exit For Next j
If y = 0 Then n = n + 1 End If Next i Print n
8.求[100,900]之间相差为12的素数对(注:要求素数对的两个素数均在该范围内)的个数。 50 n = 0
For i = 100 To 900 - 12 x = 0
For j = 2 To Sqr(i)
If i Mod j = 0 Then x = 1: Exit For Next j
If x = 0 Then y = 0
For j = 2 To Sqr(i + 12)
If (i + 12) Mod j = 0 Then y = 1: Exit For Next j
If y = 0 Then n = n + 1 End If Next i Print n
9. 一个素数(设为p)依次从最高位去掉一位,二位,三位,??,若得到的各数仍都是素数(注:1不是素数),且数p的各位数字均不为零,则称该数p为逆向超级素数。例如,617,17,7都是素数,因此617是逆向超级素数,但尽管503,03,3都是素数,但它不是逆向超级素数,因为它包含有零。试求[100,999]之内的所有逆向超级素数的和。 21645 s = 0
For i = 100 To 999 a = i \\ 10 Mod 10 b = i Mod 10
If a <> 0 And b <> 0 And b <> 1 Then x = 0
For j = 2 To Sqr(i)
If i Mod j = 0 Then x = 1: Exit For Next j
If x = 0 Then y = 0
c = i Mod 100
For j = 2 To Sqr(c)
If c Mod j = 0 Then y = 1: Exit For Next j
If y = 0 Then
第 4 页
z = 0
For j = 2 To Sqr(b)
If b Mod j = 0 Then z = 1: Exit For Next j
If z = 0 Then s = s + i End If End If End If Next i Print s
10. 一个素数(设为p)依次从最高位去掉一位,二位,三位,??,若得到的各数仍都是素数(注:1不是素数,2是素数),且数p的各位数字均不为零,则称该数p为逆向超级素数。例如,617,17,7都是素数,因此617是逆向超级素数,但尽管503,03,3都是素数,但它不是逆向超级素数,因为它包含有零。试求[100,999]之内的所有逆向超级素数从大到小数的第10个素数是多少? 797 n = 0
For i = 999 To 100 Step -1 a = i \\ 10 Mod 10 b = i Mod 10
If a <> 0 And b <> 0 And b <> 1 Then x = 0
For j = 2 To Sqr(i)
If i Mod j = 0 Then x = 1: Exit For Next j
If x = 0 Then y = 0
c = i Mod 100
For j = 2 To Sqr(c)
If c Mod j = 0 Then y = 1: Exit For Next j
If y = 0 Then z = 0
For j = 2 To Sqr(b)
If b Mod j = 0 Then z = 1: Exit For Next j
If z = 0 Then n = n + 1: If n = 10 Then s = i End If End If End If Next i Print s
11. 一个自然数是素数,且它的数字位置经过任意对换后仍为素数,则称为绝对素数。如13,试求所有两位绝对素数的和。 429 s = 0
第 5 页