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的风格实现素数筛选的更多相关文章

  1. 1341 - Aladdin and the Flying Carpet ---light oj (唯一分解定理+素数筛选)

    http://lightoj.com/volume_showproblem.php?problem=1341 题目大意: 给你矩形的面积(矩形的边长都是正整数),让你求最小的边大于等于b的矩形的个数. ...

  2. codeforces Soldier and Number Game(dp+素数筛选)

    D. Soldier and Number Game time limit per test3 seconds memory limit per test256 megabytes inputstan ...

  3. POJ 3978 Primes(素数筛选法)

    题目 简单的计算A,B之间有多少个素数 只是测试数据有是负的 //AC //A和B之间有多少个素数 //数据可能有负的!!! #include<string.h> #include< ...

  4. POJ 2689 Prime Distance (素数筛选法,大区间筛选)

    题意:给出一个区间[L,U],找出区间里相邻的距离最近的两个素数和距离最远的两个素数. 用素数筛选法.所有小于U的数,如果是合数,必定是某个因子(2到sqrt(U)间的素数)的倍数.由于sqrt(U) ...

  5. 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 ...

  6. LightOJ 1236 Pairs Forming LCM (LCM 唯一分解定理 + 素数筛选)

    http://lightoj.com/volume_showproblem.php?problem=1236 Pairs Forming LCM Time Limit:2000MS     Memor ...

  7. LightOJ 1259 Goldbach`s Conjecture (哥德巴赫猜想 + 素数筛选法)

    http://lightoj.com/volume_showproblem.php?problem=1259 题目大意:给你一个数n,这个数能分成两个素数a.b,n = a + b且a<=b,问 ...

  8. LightOJ 1341 - Aladdin and the Flying Carpet (唯一分解定理 + 素数筛选)

    http://lightoj.com/volume_showproblem.php?problem=1341 Aladdin and the Flying Carpet Time Limit:3000 ...

  9. poj 2262 Goldbach's Conjecture(素数筛选法)

    http://poj.org/problem?id=2262 Goldbach's Conjecture Time Limit: 1000MS   Memory Limit: 65536K Total ...

随机推荐

  1. std::rotate使用

    1. 进行元素范围上的左旋转 first - 原范围的起始 n_first - 应出现在旋转后范围起始的元素 last - 原范围的结尾 原来:1 2 3 左旋转后(起始元素是2) : 2 3 1

  2. JSViews--JQuery系的MVVM框架

    JSViews https://github.com/BorisMoore/jsviews JsViews: next-generation MVVM and MVP framework - brin ...

  3. JavaSE回顾及巩固的自学之路(一)——————前言

    长长的呼出一口气,终于到了末端了,JavaSE,Java入门的基础,历经一个多月,终于细细的学了一遍,大部分的时间都是熬夜到半夜两点钟.本来是一个月的课程,足足让我给学了将近两个月,为什么呢?第一,还 ...

  4. 【小玩意】time-passing-by clock

    就着youtube上的教程用html和js做了个小时钟. Code: clock.html //clock.html <!DOCTYPE html> <html> <he ...

  5. table的复制 SqlServer 数据库添加临时表(select 字段1,字段2,字段3 into)

    select  字段1,字段2,字段3 into tempname  form  table  where table.id=1; oralce : insert into MID_DRUG_DETA ...

  6. SpringBoot学习笔记<一>入门与基本配置

    毕业实习项目技术学习笔记 参考文献 学习视频 2小时学会Spring Boot:https://www.imooc.com/learn/767 学习资料 SpringBoot入门:https://bl ...

  7. [C++]数值与字符串转换问题

    #include <stdio.h>//sprintf #include <stdlib.h>//atof,atol,strtod,strtol using namespace ...

  8. Python常用模块之time模块

    python中的time和datetime模块是时间方面的模块 time模块中时间表现的格式主要有三种: 1.timestamp:时间戳,时间戳表示的是从1970年1月1日00:00:00开始按秒计算 ...

  9. <c:out>标签中的escapeXML属性

    <c:out>标签中的escapeXML属性 在<c:out>中,escapeXML属性默认为true. 当设置escapeXML的属性为true时,将value中的值以字符串 ...

  10. mysql 查询优化 ~ select count 知多少

    一 简介:今天咱们来聊聊mysql的查询总数 二 具体介绍 1 从引擎层面说 myisam  myisam的扫描总行数是非常快的,这是因为myisam会将表的总行数存储起来,定期维护,但是注意,一旦加 ...