# 素数の判定


```python
import itertools as it
from kanren import isvar, membero, var, run, eq
from kanren.core import success, fail, condeseq
from sympy.ntheory.generate import prime, isprime
```


```python
# 素数かチェックする関数
def check_prime(x):
    if isvar(x):
        return condeseq([eq(x, p)] for p in map(prime, it.count(1)))
    else:
        return success if isprime(x) else fail

x = var() 

list_nums = (23, 4, 27, 17, 13, 10, 21, 29, 3, 32, 11, 19) 
print('List of primes in the list:') 
print(set(run(0, x, (membero, x, list_nums), (check_prime, x))))
```

    List of primes in the list:
    {3, 11, 13, 17, 19, 23, 29}


`prime`は素数のこと


```python
print('List of first 7 prime numbers:')
print(set(run(13, x, (check_prime, x))))
```

    List of first 7 prime numbers:
    {2, 3, 5, 37, 7, 41, 11, 13, 17, 19, 23, 29, 31}



```python

```

8c6cecb445f8f64bf74028ba68d4ffbc5d2c29b6
ipynb
Jupyter Notebook
Ex/Chapter6/Chapter6-6.ipynb
tryoutlab/python-ai-oreilly
111a0db4a9d5bf7ec4c07b1e9e357ed4fa225f28
Unlicense
Ex/Chapter6/Chapter6-6.ipynb
tryoutlab/python-ai-oreilly
111a0db4a9d5bf7ec4c07b1e9e357ed4fa225f28
Unlicense
Ex/Chapter6/Chapter6-6.ipynb
tryoutlab/python-ai-oreilly
111a0db4a9d5bf7ec4c07b1e9e357ed4fa225f28
Unlicense
Qwen/Qwen-72B
1. YES
2. YES