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

``````
print [x for x in range(2, 100) if True not in [x % y == 0 for y in range(2, x)] ]
``````
...全文
269 点赞 收藏 24

24 条回复

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
``````

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.

``````
#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;
}``````

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] 可以么

``````
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;
}
``````

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;
}
``````

``````
print [x for x in range(2, 100) if True not in [x % y == 0 for y in range(2, x)] ]
``````

3.3w+

C/C++ 新手乐园

2013-06-23 12:20