散分收集各种语言各种版本的 "打印100以内素数" 程序

www_adintr_com 2013-06-23 12:20:16
我先抛砖引玉了, 一个 Python 的版本, 效率最低的那种:


print [x for x in range(2, 100) if True not in [x % y == 0 for y in range(2, x)] ]
...全文
269 点赞 收藏 24
写回复
24 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
www_adintr_com 2013-06-27
回复
www_adintr_com 2013-06-26
顶顶
回复
ForestDB 2013-06-25

#!/bin/sh

for i in {2..100}
do
    ((j = 2, is_prime = 1))
    while ((j < i))
    do
        if ((i % j == 0))
        then
            ((is_prime = 0))
            break
        fi
        ((j++))
    done
    if ((is_prime))
    then
        echo $i
    fi
done
回复
www_adintr_com 2013-06-25
引用 18 楼 wangdahu888 的回复:
erlang: -module(prim). -compile(export_all). isprime(P,N,B) when (B==true) -> if P==N -> B; P rem N==0 -> false; true -> isprime(P,N+1,B) end. printprime(S,E) when (S>E) -> ok; printprime(S,E)-> I=isprime(S,2,true), if I==true -> io:format("~w~n",[S]),printprime(S+1,E); I==false -> printprime(S+1,E) end.
终于看到一个不是 C/C++ 的了. 虽然我的帖子是发在 C++ 里的.
回复
赵4老师 2013-06-25
一块由“玉”构成的小行星正朝帖主头顶砸来! Finding prime numbers - Kenneth Haugland Different schemas for finding prime numbers explained with code http://www.codeproject.com/Article.aspx?tag=198374988322054872&
回复
路过学习, 14楼就是传说中的埃氏筛选算法吧~~
引用 14 楼 hugett 的回复:

#include <stdio.h>
#include <string.h>

#define N 100

int main(){
	bool isprime[N + 1];
	memset(isprime, true, sizeof(isprime));
	for(int i = 2; i * i <= N; ++i){
		if(isprime[i]){
			for(int j = i * i; j <= N; j += i) isprime[j] = false;
		}
	}
	for(int i = 2; i <= N; ++i) if(isprime[i]) printf("%d ", i);
	return 0;
}
回复
erlang: -module(prim). -compile(export_all). isprime(P,N,B) when (B==true) -> if P==N -> B; P rem N==0 -> false; true -> isprime(P,N+1,B) end. printprime(S,E) when (S>E) -> ok; printprime(S,E)-> I=isprime(S,2,true), if I==true -> io:format("~w~n",[S]),printprime(S+1,E); I==false -> printprime(S+1,E) end.
回复
rocktyt 2013-06-24
批处理版本
@echo off
setlocal enabledelayedexpansion
for /l %%i in (2,1,100) do (
set /a k = %%i / 2
set /a f = 1
	for /l %%j in (2,1,!k!) do (
	set /a p = %%i %% %%j
	if !p! == 0 (set /a f = 0
	set /a k = 0)
	)
if !f! == 1 (echo %%i)
set /a f = 1
)
回复
rocktyt 2013-06-24
模板生成版本
#include <iostream>
template<size_t N, size_t M = N/2>
struct is_prime
{
    static const bool value = N % M ? is_prime<N, M-1>::value : 0;
};
template<size_t N>
struct is_prime<N, 1>
{
    static const bool value = 1;
};
template<size_t N>
struct prime_outputer
{
    void operator()()
    {
        prime_outputer<N-1>()();
        if (is_prime<N>::value)
            std::cout<<N<<' ';
    }
};
template<>
struct prime_outputer<1>
{
    void operator()()
    {}
};
int main()
{
    prime_outputer<100>()();
    return 0;
}
回复
www_adintr_com 2013-06-24
期待更多语言的版本~~~
回复
longburulin 2013-06-23
[co[i]de=c] #include<iostream> using namespace std; int main() { int i,j,a[101]; for(i=2;i<101;i++) a[i]=0; for(i=2;i<100;i++) { if(a[i]==0) { j=i; int k=2; while(k*j<100) { a[k*j]=1; k++; } } } for(i=2;i<100;i++) if(a[i]==0) cout<<i<<" "; return 0; } [/code] 可以么
回复
www_adintr_com 2013-06-23
引用 2 楼 fetag 的回复:
你可以试试判断这个几个数是否为素数~~ 64167923538537646859730428902332621824680684450335276198844853437211192581741493192824496354392029688858110505951057933521693435675109057185724371955738384042585564928166279957493792697274321778587380781130190508909882379530116194704348642742343892529960367041277051993838196911215612498556072728470612839709 77496647029274319469305393849720372253945267325722789832503217339405524146944760314343653966847675664071682020837100328377375125137078956721642505110710239249991457980460029891302374191088676470266038520066360709193096618027838439963907798908129650493772231765714854054675653200965455562952265068313054426363 31592965210295742976365098353880834195601134241223785754912138330639054249828343032137652595943243180936607258215245778992517330101470431352355507803588766288574356894487817076884964018012957023529688209098047040668956435612699132759789586157128764164454449785773959394658340764111541602068933466520413506311753084727129566285297553587906737504607286502150987016304360892997542516113227846896595073366298537963031966058038270390106776572452494154177171932572203278125408794805582154780087779102570078130847294193602248810563660127104905788195248803763066810079490008199956056251655117474670992375061703762648469997739
这样就不是散分的目的了,是搞密码破解的了
回复
橡木疙瘩 2013-06-23
我写个C++版的,速度超快的:

int main()
{
 static const int prime[] = { 
    2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97
  };
  static const int count = sizeof(prime) / sizeof(prime[0]);
  std::copy( &prime[0], &prime[count], std::ostream_iterator<int>( std::cout, "\t" ) );
  return 0;
}
回复
独孤过儿 2013-06-23
你可以试试判断这个几个数是否为素数~~ 64167923538537646859730428902332621824680684450335276198844853437211192581741493192824496354392029688858110505951057933521693435675109057185724371955738384042585564928166279957493792697274321778587380781130190508909882379530116194704348642742343892529960367041277051993838196911215612498556072728470612839709 77496647029274319469305393849720372253945267325722789832503217339405524146944760314343653966847675664071682020837100328377375125137078956721642505110710239249991457980460029891302374191088676470266038520066360709193096618027838439963907798908129650493772231765714854054675653200965455562952265068313054426363 31592965210295742976365098353880834195601134241223785754912138330639054249828343032137652595943243180936607258215245778992517330101470431352355507803588766288574356894487817076884964018012957023529688209098047040668956435612699132759789586157128764164454449785773959394658340764111541602068933466520413506311753084727129566285297553587906737504607286502150987016304360892997542516113227846896595073366298537963031966058038270390106776572452494154177171932572203278125408794805582154780087779102570078130847294193602248810563660127104905788195248803763066810079490008199956056251655117474670992375061703762648469997739
回复
独孤过儿 2013-06-23
100以内的太没意思了,1024 bits以上,这个还值得一玩
回复
hugett 2013-06-23

#include <stdio.h>
#include <string.h>

#define N 100

int main(){
	bool isprime[N + 1];
	memset(isprime, true, sizeof(isprime));
	for(int i = 2; i * i <= N; ++i){
		if(isprime[i]){
			for(int j = i * i; j <= N; j += i) isprime[j] = false;
		}
	}
	for(int i = 2; i <= N; ++i) if(isprime[i]) printf("%d ", i);
	return 0;
}
回复
流星陨落 2013-06-23
回复
回复
图灵狗 2013-06-23
参考我的博文http://blog.csdn.net/turingo/article/details/8161061
引用 楼主 adlay 的回复:
我先抛砖引玉了, 一个 Python 的版本, 效率最低的那种:

print [x for x in range(2, 100) if True not in [x % y == 0 for y in range(2, x)] ]
回复
www_adintr_com 2013-06-23
回复
加载更多回复
相关推荐
发帖
新手乐园
创建于2007-09-28

3.3w+

社区成员

C/C++ 新手乐园
申请成为版主
帖子事件
创建了帖子
2013-06-23 12:20
社区公告
暂无公告