用Go的风格实现素数筛选
package main import (
"fmt"
"time"
) const End = func source(ch chan<- int) {
for i := ; i < End; i++ {
ch <- i
}
} func validate(in <-chan int, out chan<- int, fix int) {
for {
select {
case i := <-in:
if i%fix != {
out <- i
}
case <-time.After(1 * time.Second):
close(out)
return
}
}
} // 打印素数
func main() { // 逐个获取待检测的数据源
ch := make(chan int)
go source(ch) for {
data, ok := <-ch
if !ok {
break
}
fmt.Println(data)
out := make(chan int)
go validate(ch, out, data)
ch = out
}
}
参考:http://tonybai.com/2017/04/20/go-coding-in-go-way/ , 但他实现的没有关闭chan, 导致 fatal error: all goroutines are asleep - deadlock! ,本方法优化了这个BUG.


用Go的风格实现素数筛选的更多相关文章
- 1341 - Aladdin and the Flying Carpet ---light oj (唯一分解定理+素数筛选)
http://lightoj.com/volume_showproblem.php?problem=1341 题目大意: 给你矩形的面积(矩形的边长都是正整数),让你求最小的边大于等于b的矩形的个数. ...
- codeforces Soldier and Number Game(dp+素数筛选)
D. Soldier and Number Game time limit per test3 seconds memory limit per test256 megabytes inputstan ...
- POJ 3978 Primes(素数筛选法)
题目 简单的计算A,B之间有多少个素数 只是测试数据有是负的 //AC //A和B之间有多少个素数 //数据可能有负的!!! #include<string.h> #include< ...
- POJ 2689 Prime Distance (素数筛选法,大区间筛选)
题意:给出一个区间[L,U],找出区间里相邻的距离最近的两个素数和距离最远的两个素数. 用素数筛选法.所有小于U的数,如果是合数,必定是某个因子(2到sqrt(U)间的素数)的倍数.由于sqrt(U) ...
- algorithm@ Sieve of Eratosthenes (素数筛选算法) & Related Problem (Return two prime numbers )
Sieve of Eratosthenes (素数筛选算法) Given a number n, print all primes smaller than or equal to n. It is ...
- LightOJ 1236 Pairs Forming LCM (LCM 唯一分解定理 + 素数筛选)
http://lightoj.com/volume_showproblem.php?problem=1236 Pairs Forming LCM Time Limit:2000MS Memor ...
- LightOJ 1259 Goldbach`s Conjecture (哥德巴赫猜想 + 素数筛选法)
http://lightoj.com/volume_showproblem.php?problem=1259 题目大意:给你一个数n,这个数能分成两个素数a.b,n = a + b且a<=b,问 ...
- LightOJ 1341 - Aladdin and the Flying Carpet (唯一分解定理 + 素数筛选)
http://lightoj.com/volume_showproblem.php?problem=1341 Aladdin and the Flying Carpet Time Limit:3000 ...
- poj 2262 Goldbach's Conjecture(素数筛选法)
http://poj.org/problem?id=2262 Goldbach's Conjecture Time Limit: 1000MS Memory Limit: 65536K Total ...
随机推荐
- IP简介(一)
1. OSI模型 TCP是TCP/IP的第三层传输层,对应OSI的第四层传输层: IP是TCP/IP的第二层互联层,对应OSI的第三层网络层. TCP属于OSI中的运输层它是面向连接的协议: IP属于 ...
- VS Code +node npm 调试 js
打开vsCode的调试控制台里面的终端 然后输入下面代码 npm install express-generator -g 1 创建一个命名为 myapp 的应用. express myapp 你就可 ...
- 服务器部署全程记录(centos6.5)
1.安装nginx 上传安装包:put E:\yz_index\installPackage\nginx-1.14.0.tar.gz 解压:tar zxvf nginx-1.14.0.tar.gz 切 ...
- SpringBoot系列: SpringBoot 启动慢的问题
SpringBoot 应用启动速度往往很快, 但在某些Linux 服务器上可能会很慢, 可能超过1分钟, 有时候甚至启动不起来. 下面过程耗时太长:IdGeneratorBase: Creation ...
- C++获取MAC与IP
#include <Nb30.h> #pragma comment(lib,"ws2_32.lib") #pragma comment(lib,"netapi ...
- Hibernate常用API以及使用说明
1===>Hibernate常用的aip有Configuration,SessionFactory,Transaction,Session Configuration主要用于加载配置文件,使用 ...
- Spring使用事务增加的注解实现方
以下是我的文件结构 步骤1:配置数据源 <bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDa ...
- ads出现村田电容电感无法仿真的问题解决(`BJT1' is an instance of an undefined model `BJTM1')
需要的控件是 murata include,该控件是跟随村田库一起倒入ADS中的
- faster rcnn相关内容
转自: https://zhuanlan.zhihu.com/p/31426458 faster rcnn的基本结构 Faster RCNN其实可以分为4个主要内容: Conv layers.作为一种 ...
- c# cbo控件
c# cbo控件 ,要获取选中的值 最好使用 ((强转的类型)selectitem).属性