用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 ...
随机推荐
- 【leetcode-66】 加一
给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一. 最高位数字存放在数组的首位, 数组中每个元素只存储一个数字. 你可以假设除了整数 0 之外,这个整数不会以零开头. 示例 1: 输入 ...
- 致备战noip2018的勇士
戒骄戒躁,自律当先.稳扎稳打,颗粒归仓.目标高远,落实当下.无惧将来,无畏过往.愿你们承担一份荣耀,愿你们书写一段辉煌.——致备战noip2018的你们 ——退役学姐致gryz备战中的勇士 ——lpy ...
- 前端面试题整理—ES6篇
1.es5和es6的区别,说一下你所知道的es6 ECMAScript5,即ES5,是ECMAScript的第五次修订,于2009年完成标准化 ECMAScript6,即ES6,是ECMAScript ...
- 054、准备 macvlan环境(2019-03-21 周四)
参考https://www.cnblogs.com/CloudMan6/p/7352620.html 除了overlay,docker还开发了另一个支持跨主机容器网络的 driver :macvl ...
- HanLP 关键词提取算法分析
HanLP 关键词提取算法分析 参考论文:<TextRank: Bringing Order into Texts> TextRank算法提取关键词的Java实现 TextRank算法自动 ...
- AndroidStudio替换空行
(1)在Edit Replace In Path输入框中输入:^\s*\n (\s代表任何空白字符,\S代表任何非空白字符,*代表任意个数,\n匹配换行符) (2)Replace With输入框的值为 ...
- Servlet 快速开始 表单中文字段
req.getParameter | getParameterValue 一大特点是 返回null表示没有. [web.xml] <servlet> <serlvlet-mappin ...
- PHP中使用jQuery+Ajax实现分页查询多功能操作
1.首先做主页面Ajax_pag.php 代码如下: <!DOCTYPE html> <html> <head> <meta charset="UT ...
- C++ 窗口
DestroyWindow(); //销毁窗口 可重载的事件: PostNcDestroy 窗口销毁后调用
- [C++]Knights of a Polygonal Table(骑士的多角桌)
[程序结果:用例未完全通过,本博文仅为暂存代码之目的] /* B. Knights of a Polygonal Table url:http://codeforces.com/problemset/ ...