Dada uma lista de números inteiros positivos, determine se cada par adjacente de números inteiros compartilha um fator primo. Em outras palavras, produza verdade se e somente se não houver dois números inteiros vizinhos na lista como primos.
Em outros termos: dada uma lista de números inteiros positivos [a 1 a 2 … a n ] , produza se
gcd (a 1 , a 2 )> 1 && gcd (a 2 , a 3 )> 1 &&… && gcd (a n-1 , a n )> 1.
A lista sempre conterá pelo menos dois elementos (n ≥ 2).
Contudo…
Esse desafio também é de fonte restrita : os pontos de código em sua resposta (qualquer que seja a página de código) deve satisfazer a condição que seu programa verifica.
Por exemplo, print 2
é um programa válido. Como uma lista de pontos de código Unicode, é [112 114 105 110 116 32 50] , que satisfaz esta condição: 112 e 114 compartilham um fator 2 ; e 114 e 105 compartilham um fator de 3 etc.
No entanto, nãomain
pode ocorrer em um programa válido (desculpe!), Pois os pontos de código Unicode e , nomeadamente 109 e 97 , são coprime. (Felizmente, sua inscrição não precisa ser um programa completo!)m
a
Seu programa não tem permissão para conter o ponto de código 0.
Casos de teste
Verdade:
[6 21] -> 1
[502 230 524 618 996] -> 1
[314 112 938 792 309] -> 1
[666 642 658 642 849 675 910 328 320] -> 1
[922 614 530 660 438 854 861 357 477] -> 1
Falsy:
[6 7] -> 0
[629 474 502 133 138] -> 0
[420 679 719 475 624] -> 0
[515 850 726 324 764 555 752 888 467] -> 0
[946 423 427 507 899 812 786 576 844] -> 0
Este é o código-golfe : o código mais curto em bytes vence.
%)+/5;=CGIOSYaegkmq\DEL
.print 2
era válido, mas);=ae
ser privilegiado é realmente difícil, não considerei isso ... Será que algo como Haskell pode competir?Respostas:
MATL , 14 bytes
Isso gera um vetor de coluna não vazio de números diferentes de zero como verdade ou um vetor que contém pelo menos uma entrada zero como falso.
Try it online!
Verify all test cases. The footer code contains an
if
branch to test truthyness/falsyness.Since MATL uses ASCII, the source code is encoded as
which satisfies the requirement.
Explanation
fonte
Haskell,
103100 bytesEDIT:
d<-fz
guard to merge and shorten the last two lines.f
is the main function, which takes a list of integers and returns aBool
.Note that the first two
ԁ
s (only) are Cyrillic (Komi) Unicode characters, and there's a tab character before the first one.Try it online! or test it on itself.
How it works
f
is the main function. All it does is wrap its argumentԁ
in a singleton list (because the prime ASCII value of)
makes parentheses much more awkward to use than square brackets) and callzb
with that and a dummy argument (the Haskell functionid
happens to have just the right characters to fit here).=]
is impossible with plain ASCII, so the argument is named with the 2-byte Unicode characterCYRILLIC SMALL LETTER KOMI DE (ԁ)
, codepoint value3*7*61=U+0501
, which fits with all of those and[
.f fz|bf<-fz=zb[bf]fz
.zb
takes two arguments, a singleton list whose element is the real list of numbers being recursed on, and a dummy argumentfz
needed only to get az
before the function's=
s.z
is called with the first two (namedh
andp
), and if that returnsTrue
,zb
recurses on the tailp:l
of the list.zb
returnsTrue
. Since=
needs to be followed by the characterz
, the simplest way to do this is to use a call of thez
function that itself is known to returnTrue
.z
takes two arguments and recursively calculates their greatest common divisor using subtraction (every other relevant integer division or gcd function is unavailable), returningTrue
if it's greater than one.0
, with the second argument being the gcd. On this line the second argument is also namedz
. The character1
is awkward here soz^0
is used to get the number one.f
is smaller than the secondfz
, they are swapped andz
recurses.z
recurses (also swapping the arguments, although that's just to avoid parentheses.)fonte
05AB1E, 8 bytes
Code
Uses the 05AB1E encoding, which gives us the following list of code points:
Try it online! or Verify the source code!
Explanation
Since the gcd operator (
¿
) has a prime code point I had to look for other ways to check coprimality:fonte
Ò
overf
?Husk, 8 bytes
For Truthy inputs it returns a positive integer, for Falsy it returns 0
Try it online! and Tested on its own codepoints
Uses Husk's codepage
Explanation
fonte
Ṡ
? I see it on the docs too in the "type" column on the commands page and can't get my head around it so want to look it up so I might be able to learn it.Ṡ
in Haskell's syntax. It translates roughly toṠ(f,g,x) = f(g(x),x)
in more mainstream languages.`Ṡ g f x = Ṡ f g x = f (g x) x
Japt,
87 bytesTest it online!
Code points:
Explanation
fonte
Jelly,
119 bytesSaved 2 bytes thanks to @Jonathan Allan.
Try it online!
Jelly has its own code-page and the codepoints of each character are
This tests for non-coprime numbers by checking if
lcm(a, b) != a*b
. There might be a shorter solution as I just filtered for characters with even codepoints.Explanation
fonte
,
is even so you can doæln,P¥ð2\
for two less. Edit: I dropped the trailingP
, make that one less :p)TI-BASIC, 38 bytes
TI-BASIC is tokenized into one- or two-byte tokens, as listed here.
The trickiest parts of this solution were:
The comma token is a prime number (43), forcing me to surround it with multiples of 43 (in this case the V token, which is 86).
The gcd( token is a large prime number (47881), which means it couldn't be used at all.
The tokens for this program come out to:
Explanation
fonte
Pyth, 15 bytes
Try it here or Check out Test Suite.
This is a collaborative effort between Erik the Outgolfer and Mr. Xcoder. Returns an inconsistent value (non-empty list) for truthy, and the empty list for falsy.
ASCII-values
Which share the following factors:
Explanation
Without the restricted-source requirement, this would've been a
75-byte version accomplishing the same task (-2 thanks to FryAmTheEggman):Explanation
fonte
Q
s at the end?.b
has variable arities, and using implicit input means it will pick the lowest (1) instead of then intended (2).