"에라토스테네스의 체 구현 함수 eratosthenes()"의 두 판 사이의 차이

잔글 (봇: 자동으로 텍스트 교체 (-source +syntaxhighlight))
 
(다른 사용자 한 명의 중간 판 19개는 보이지 않습니다)
4번째 줄: 4번째 줄:
;함수 eratosthenes()
;함수 eratosthenes()
;함수 primes_sieve()
;함수 primes_sieve()
==Java==
[[분류: Java]]
<syntaxhighlight lang='java'>
import java.util.Arrays;
public class MyClass {
    public static void main(String[] args) {
    int MAX = 17;
    int[] prime = new int[MAX+1];
    Arrays.fill(prime, 1);
    prime[0] = 0;
    prime[1] = 0;
    for(int i=2; i<=Math.sqrt(MAX); i++) {
    for(int j=i*2; j<=MAX; j+=i) prime[j] = 0;
    }
    System.out.println(Arrays.toString(prime));
    // [0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1]
    for(int i=1; i<=MAX; i++) {
    if( prime[i] == 1 )
    System.out.format("%d ", i );
    // 2 3 5 7 11 13 17
    }
    }
}
</syntaxhighlight>
<syntaxhighlight lang='java'>
import java.util.*;
public class MyClass {
static boolean[] primes_sieve(int max) {
boolean[] a = new boolean[max+1];
Arrays.fill(a, Boolean.TRUE);
a[0] = a[1] = false;
int i, j;
for(i=2; i<=max; i++) {
for(j=i*2; j<=max; j+=i) a[j] = false;
}
return a;
}
public static void main(String[] args) {
System.out.println( Arrays.toString(primes_sieve(10)) );
// [false, false, true, true, false, true, false, true, false, false, false]
System.out.println( Arrays.toString(primes_sieve(11)) );
// [false, false, true, true, false, true, false, true, false, false, false, true]
}
}
</syntaxhighlight>
<syntaxhighlight lang='java'>
import java.util.*;
public class MyClass {
static boolean[] primes_sieve(int max) {
boolean[] a = new boolean[max+1];
Arrays.fill(a, Boolean.TRUE);
a[0] = a[1] = false;
int i, j;
for(i=2; i<=Math.sqrt(max); i++) {
for(j=i*2; j<=max; j+=i) a[j] = false;
}
return a;
}
public static void main(String[] args) {
System.out.println( Arrays.toString(primes_sieve(10)) );
// [false, false, true, true, false, true, false, true, false, false, false]
System.out.println( Arrays.toString(primes_sieve(11)) );
// [false, false, true, true, false, true, false, true, false, false, false, true]
}
}
</syntaxhighlight>


==PHP==
==PHP==
[[분류: PHP]]
[[분류: PHP]]
<source lang='php'>
<syntaxhighlight lang='php'>
<?php
<?php
function primes_sieve($size) {
function primes_sieve($size) {
     $a = array_fill(0,$size+1,false);
     $a = array_fill(0,$size+1,true);
     for($i=2; $i<$size; $i++) $a[$i] = true;
     $a[0] = $a[1] = false;
    $sqrt_size = sqrt($size);
     for($i=2; $i<=sqrt($size); $i++) {
     for($i=2; $i<$sqrt_size; $i++) {
         for($j=$i*2; $j<=$size; $j+=$i) $a[$j] = false;
         for($j=$i*2; $j<$size; $j+=$i) $a[$j] = false;
     }
     }
     return $a;
     return $a;
}
}
echo json_encode( primes_sieve(10) );
$sieve = primes_sieve(10);
echo json_encode( $sieve ) . "\n";
# [false,false,true,true,false,true,false,true,false,false,false]
# [false,false,true,true,false,true,false,true,false,false,false]
print_r( primes_sieve(10) );
$sieve = primes_sieve(11);
# Array
echo json_encode( $sieve ) . "\n";
# (
# [false,false,true,true,false,true,false,true,false,false,false,true]
#     [0] =>
</syntaxhighlight>
#    [1] =>
#    [2] => 1
#    [3] => 1
#    [4] =>
#    [5] => 1
#    [6] =>
#    [7] => 1
#    [8] =>
#    [9] =>
#    [10] =>
# )
</source>


==Python==
==Python==
<source lang='python'>
<syntaxhighlight lang='python'>
def primes_sieve(size):
def primes_sieve(size):
     a = [False]*(size+1)
     a = [False]*2 + [True]*(size-1)
    for i in range(2,size):
        a[i]=True
     for i in range(2,int(size**.5)+1):
     for i in range(2,int(size**.5)+1):
         a[i*2::i]=[False]*((size-i)//i)
         a[i*2::i]=[False]*((size-i)//i)
     return a
     return a
sieve = primes_sieve(10)
print( primes_sieve(10) )
print( sieve )
# [False, False, True, True, False, True, False, True, False, False, False]
# [False, False, True, True, False, True, False, True, False, False, False]
for i in range(1,11):
print( primes_sieve(11) )
    print( i, sieve[i] )
# [False, False, True, True, False, True, False, True, False, False, False, True]
# 1 False
</syntaxhighlight>
# 2 True
# 3 True
# 4 False
# 5 True
# 6 False
# 7 True
# 8 False
# 9 False
# 10 False
</source>


==같이 보기==
==같이 보기==
* [[에라토스테네스의 체 구현 클래스 Eratosthenes]]
* [[에라토스테네스의 체]]
* [[에라토스테네스의 체]]
* [[함수 is_prime()]]
* [[함수 is_prime()]]
* [[함수 primes()]]

2020년 11월 2일 (월) 02:36 기준 최신판

1 개요[ | ]

에라토스테네스의 체 구현
함수 eratosthenes()
함수 primes_sieve()

2 Java[ | ]

import java.util.Arrays;
public class MyClass {
    public static void main(String[] args) {
    	int MAX = 17;
    	int[] prime = new int[MAX+1];
    	Arrays.fill(prime, 1);
    	prime[0] = 0;
    	prime[1] = 0;
    	for(int i=2; i<=Math.sqrt(MAX); i++) {
    		for(int j=i*2; j<=MAX; j+=i) prime[j] = 0;
    	}
    	System.out.println(Arrays.toString(prime));
    	// [0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1]
    	for(int i=1; i<=MAX; i++) {
    		if( prime[i] == 1 )
    			System.out.format("%d ", i );
    			// 2 3 5 7 11 13 17 
    	}
    }
}
import java.util.*;
public class MyClass {
	static boolean[] primes_sieve(int max) {
		boolean[] a = new boolean[max+1];
		Arrays.fill(a, Boolean.TRUE);
		a[0] = a[1] = false;
		int i, j;
		for(i=2; i<=max; i++) {
			for(j=i*2; j<=max; j+=i) a[j] = false;
		}
		return a;
	}
	public static void main(String[] args) {
		System.out.println( Arrays.toString(primes_sieve(10)) );
		// [false, false, true, true, false, true, false, true, false, false, false]
		System.out.println( Arrays.toString(primes_sieve(11)) );
		// [false, false, true, true, false, true, false, true, false, false, false, true]
	}
}
import java.util.*;
public class MyClass {
	static boolean[] primes_sieve(int max) {
		boolean[] a = new boolean[max+1];
		Arrays.fill(a, Boolean.TRUE);
		a[0] = a[1] = false;
		int i, j;
		for(i=2; i<=Math.sqrt(max); i++) {
			for(j=i*2; j<=max; j+=i) a[j] = false;
		}
		return a;
	}
	public static void main(String[] args) {
		System.out.println( Arrays.toString(primes_sieve(10)) );
		// [false, false, true, true, false, true, false, true, false, false, false]
		System.out.println( Arrays.toString(primes_sieve(11)) );
		// [false, false, true, true, false, true, false, true, false, false, false, true]
	}
}

3 PHP[ | ]

<?php
function primes_sieve($size) {
    $a = array_fill(0,$size+1,true);
    $a[0] = $a[1] = false;
    for($i=2; $i<=sqrt($size); $i++) {
        for($j=$i*2; $j<=$size; $j+=$i) $a[$j] = false;
    }
    return $a;
}
$sieve = primes_sieve(10);
echo json_encode( $sieve ) . "\n";
# [false,false,true,true,false,true,false,true,false,false,false]
$sieve = primes_sieve(11);
echo json_encode( $sieve ) . "\n";
# [false,false,true,true,false,true,false,true,false,false,false,true]

4 Python[ | ]

def primes_sieve(size):
    a = [False]*2 + [True]*(size-1)
    for i in range(2,int(size**.5)+1):
        a[i*2::i]=[False]*((size-i)//i)
    return a
print( primes_sieve(10) )
# [False, False, True, True, False, True, False, True, False, False, False]
print( primes_sieve(11) )
# [False, False, True, True, False, True, False, True, False, False, False, True]

5 같이 보기[ | ]

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}