Coprime triplets
Coprime triplets is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
- Task
Find and show the smallest number which is coprime to the last two predecessors and has not yet appeared; a(1)=1, a(2)=2.
p and q are coprimes if they have no common factors other than 1.
Let p, q < 50
Phix
function coprime_triplets(integer less_than=50) sequence cpt = {1,2} while true do integer m = 1 while find(m,cpt) or gcd(m,cpt[$])!=1 or gcd(m,cpt[$-1])!=1 do m += 1 end while if m>=less_than then exit end if cpt &= m end while return cpt end function sequence res = apply(true,sprintf,{{"%2d"},coprime_triplets()}) printf(1,"Found %d coprime triplets:\n%s\n",{length(res),join_by(res,1,10," ")})
- Output:
Found 36 coprime triplets: 1 2 3 5 4 7 9 8 11 13 6 17 19 10 21 23 16 15 29 14 25 27 22 31 35 12 37 41 18 43 47 20 33 49 26 45
Ring
<lang ring> see "working..." + nl row = 2 numbers = 1:50 first = 1 second = 2 see "Coprime triplets are:" + nl see "" + first + " " + second + " "
for n = 3 to len(numbers) flag1 = 1 flag2 = 1 if first < numbers[n] min = first else min = numbers[n] ok for m = 2 to min if first%m = 0 and numbers[n]%m = 0 flag1 = 0 exit ok next if second < numbers[n] min = second else min = numbers[n] ok for m = 2 to min if second%m = 0 and numbers[n]%m = 0 flag2 = 0 exit ok next if flag1 = 1 and flag2 = 1 see "" + numbers[n] + " " first = second second = numbers[n] del(numbers,n) row = row+1 if row%10 = 0 see nl ok n = 2 ok next
see nl + "Found " + row + " coprime triplets" + nl see "done..." + nl
</lang>
- Output:
working... Coprime triplets are: 1 2 3 5 4 7 9 8 11 13 6 17 19 10 21 23 16 15 29 14 25 27 22 31 35 12 37 41 18 43 47 20 33 49 26 45 Found 36 coprime triplets done...