Sort primes from list to a list: Difference between revisions
Content added Content deleted
m (julia example) |
|||
Line 86: | Line 86: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
===Procedural=== |
|||
<lang python> |
|||
print("working...") |
<lang python>print("working...") |
||
print("Primes are:") |
print("Primes are:") |
||
Line 105: | Line 105: | ||
Temp.sort() |
Temp.sort() |
||
print(Temp) |
print(Temp) |
||
print("done...") |
print("done...")</lang> |
||
⚫ | |||
{{out}} |
{{out}} |
||
<pre> |
<pre>working... |
||
working... |
|||
Primes are: |
Primes are: |
||
[2, 7, 13, 43, 103] |
[2, 7, 13, 43, 103] |
||
done... |
done...</pre> |
||
</pre> |
|||
===Functional=== |
|||
<lang python>'''Prime elements in rising order''' |
|||
# primeElementsSorted :: [Int] -> [Int] |
|||
def primeElementsSorted(xs): |
|||
'''The prime elements of xs in rising order''' |
|||
return sorted(x for x in xs if isPrime(x)) |
|||
# ------------------------- TEST ------------------------- |
|||
# main :: IO () |
|||
def main(): |
|||
'''Filtered elements of given list in rising order''' |
|||
print( |
|||
primeElementsSorted([ |
|||
2, 43, 81, 122, 63, 13, 7, 95, 103 |
|||
]) |
|||
) |
|||
# ----------------------- GENERIC ------------------------ |
|||
# isPrime :: Int -> Bool |
|||
def isPrime(n): |
|||
'''True if n is prime.''' |
|||
if n in (2, 3): |
|||
return True |
|||
if 2 > n or 0 == n % 2: |
|||
return False |
|||
if 9 > n: |
|||
return True |
|||
if 0 == n % 3: |
|||
return False |
|||
def p(x): |
|||
return 0 == n % x or 0 == n % (2 + x) |
|||
return not any(map(p, range(5, 1 + int(n ** 0.5), 6))) |
|||
# MAIN --- |
|||
if __name__ == '__main__': |
|||
⚫ | |||
{{Out}} |
|||
<pre>[2, 7, 13, 43, 103]</pre> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |