用C#语言求300以内的素数
2015-10-04 23:09
471 查看
程序思想是用这个数去除2到sqrt(这个数),如果能被整除,就不是素数。斜体加粗部分就是定义一个IsSuShu(int x)函数来判断是否整除。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _90
{
class Program
{
***static private Boolean IsSuShu(int x)
{
Boolean Yes = true;
for (int i = 2; i < Math.Sqrt(x); i++)
{
if ((x % i) == 0)
{
Yes = false;
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _90
{
class Program
{
***static private Boolean IsSuShu(int x)
{
Boolean Yes = true;
for (int i = 2; i < Math.Sqrt(x); i++)
{
if ((x % i) == 0)
{
Yes = false;
}
} return Yes; }*** static void Main(string[] args) { for(int i=2;i<300;i++) { if(IsSuShu(i)==true) { Console.Write(i.ToString()); Console.Write(" "); } } Console.ReadKey(); } }
}
相关文章推荐
- C#将原工程中的WinForm窗体添加到新工程中
- 用C#开发了四天的UWP应用有感
- webform C# log4net不起作用 5种 原因
- C#——.net背景和第一个C#程序
- 从控制台读取password - C#
- 【C#】——抽象类VS接口
- C# WinForm开发系列 - DataGridView 使用方法集锦
- C#学习日记09---数据类型 之 结构(Struct)类型
- C#拓展方法
- C#隐式执行CMD命令
- C# 获得当前路径(目录)的方法
- C#装箱和拆箱原理
- .NET4.0,C#泛型接口的协变与抗变
- C# 中Parallel类的使用
- 【C#】值类型VS引用类型and现金VS存折
- c#多维数组
- C#字符串比较
- C#学习日记08---数据类型 之 字符类型
- C#字符串和正则表达式
- C#学习日记07---数据类型 之 decimal类型