Almost prime: Difference between revisions
Content deleted Content added
Added Gambas |
imported>Maxima enthusiast No edit summary |
||
Line 3,128: | Line 3,128: | ||
k = 4: 16 24 36 40 54 56 60 81 84 88 |
k = 4: 16 24 36 40 54 56 60 81 84 88 |
||
k = 5: 32 48 72 80 108 112 120 162 168 176</pre> |
k = 5: 32 48 72 80 108 112 120 162 168 176</pre> |
||
=={{header|Maxima}}== |
|||
<syntaxhighlight lang="maxima"> |
|||
/* Predicate function that checks k-almost primality for given integer n and parameter k */ |
|||
k_almost_primep(n,k):=if integerp((n)^(1/k)) and primep((n)^(1/k)) then true else |
|||
lambda([x],(length(ifactors(x))=k and unique(map(second,ifactors(x)))=[1]) or (length(ifactors(x))<k and apply("+",map(second,ifactors(x)))=k))(n)$ |
|||
/* Function that given a parameter k1 returns the first len k1-almost primes */ |
|||
k_almost_prime_count(k1,len):=block( |
|||
count:0, |
|||
while length(sublist(makelist(i,i,count),lambda([x],k_almost_primep(x,k1))))<len do (count:count+1), |
|||
sublist(makelist(i,i,count),lambda([x],k_almost_primep(x,k1))))$ |
|||
/* Test cases */ |
|||
k_almost_prime_count(1,10); |
|||
k_almost_prime_count(2,10); |
|||
k_almost_prime_count(3,10); |
|||
k_almost_prime_count(4,10); |
|||
k_almost_prime_count(5,10); |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
[2,3,5,7,11,13,17,19,23,29] |
|||
[4,6,9,10,14,15,21,22,25,26] |
|||
[8,12,18,20,27,28,30,42,44,45] |
|||
[16,24,36,40,54,56,60,81,84,88] |
|||
[32,48,72,80,108,112,120,162,168,176] |
|||
</pre> |
|||
=={{header|Modula-2}}== |
=={{header|Modula-2}}== |