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. jQuery使用(五):DOM操作之插入和删除元素

    插入: insertBofore() before() insertAfter() after() appendTo() append() prependTo() prepen() 删除: remov ...

  2. tedu训练营day03

    Day03笔记1.作业 1.假如你现在25周岁,每年365天,计算你过了多少个星期天(大概数字) 提示 :地板除 2.毕业薪资为10000元,每年涨20%,十年之后你的薪资为多少元? 提示: 幂运算( ...

  3. javascript中var、let和const的区别

    在javascript中,var.let和const都可以用来声明变量,那么三者有什么区别呢?要回答这个问题,我们可以从先想想:三种不同的声明会影响变量的哪些方面?这些方面也就是变量的特性,那么变量有 ...

  4. 【二】Spring Cloud 入门

    官网 版本号: SpringCloud中文网:https://springcloud.cc SpringCloud中文社区:http://springcloud.cn 以下代码就是Maven父子工程, ...

  5. XXE攻防

    一.XML基础知识 XML用于标记电子文件使其具有结构性的标记语言,可以用来标记数据.定义数据类型,是一种允许用户对自己的标记语言进行定义的源语言.XML文档结构包括XML声明.DTD文档类型定义(可 ...

  6. Js调用asp.net后台代码

    方法一:         1.首先建立一个按钮,在后台将调用或处理的内容写入button_click中; 2.在前台写一个js函数,内容为document.getElementById("b ...

  7. (一)python的前世今生

    一:Python介绍 python的创始人为吉多·范罗苏姆(Guido van Rossum)(目前还活着),诞生于1989年,是一个脚本解释程序,由于python语言结构优美,清晰简单,随着人工智能 ...

  8. 开发mis系统需要的技术

    一.b/s架构 b/s架构:就broser/server,浏览器/服务器的说法.服务器端要运行tomcat,提供链接数据库服务供java代码读写数据,这个可以在eclipse中配置运行.浏览器则解释j ...

  9. Python之线程 1 - 线程基本概念

    一 背景知识 1.进程 2.有了进程为什么还要线程 3.线程的出现 二 线程和进程的关系 三 线程的特点 四 线程的实际应用场景 五 内存中的线程 六 用户级线程和内核级线程(了解) 1.用户级线程 ...

  10. kvm 搭建

    一,准备环境   物理机 虚拟机 操作系统 CentOS 6.8 x64 CentOS 6.8 x64 CPU/内存 10核超线程x2/64G 2核/4G 外网IP -- 内网IP eth1_192. ...