MuPAD 113 - 8 = 105

1

MuPAD 113 - 8 = 105

g:=[0,ithprime(i)$i=1..n]:f:=_for_in:f(l,g,f(d,g,f(b,g,f(a,g,f(c,g,if l+d+b+a+c=n then print(l,d,b,a,c)end)))))

Ta wersja wydrukuje również wszystkie permutacje każdego rozwiązania:

0, 0, 0, 0, 7
0, 0, 0, 2, 5
0, 0, 0, 5, 2
0, 0, 0, 7, 0
0, 0, 2, 0, 5
...

I tak, tworzy zbyt długą listę g. Kogo to obchodzi? :-)

Wersja bez golfa:

g:=[0].select([$1..n],isprime):
for l in g do
  for d in g do
    for b in g do
      for a in g do
        for c in g do
          if l+d+b+a+c=n then print(l,d,b,a,c); end;
        end
      end
    end
  end
end
Christopher Creutzig
źródło
Nie mam dostępu do mupada - czy ktoś może sprawdzić, czy to działa?
kabina

Odpowiedzi:

1

Galaretka , 19 bajtów (ale bardzo wolno - potrzebna rada)

ÆR;0x5Œ!ḣ€5¹©S€i³ị®

Wypróbuj online!

ÆR;0x5Œ!ḣ€5¹©€i³ị®     main link, takes one argument N
ÆR                     get all the primes less than N
  ;0x5                 add zero, and then repeat the entire list 5 times
      Œ!               get all the permutations of this huge list (takes a long time!)
        ḣ€5            for each permutation, just take the first 5 numbers
                       (this gives us all possible length-5 combinations of the primes plus zero, with some repeats)
           ¹©          save that list to register
              S€       take the sum of every permutation in the list...
                i³     and find the index of our first argument N in that list of sums
                  ị®   then recall our list of permutations, and get the correct permutation at that index!

Jeśli masz jakieś pomysły, aby uczynić go szybszym i krótszym, daj mi znać!

Złupić
źródło
1
12 bajtów . ṗЀ5produkuje wszystkie kombinacje liczb pierwszych o długości od jednego do pięciu. S=¥sprawdza, czy suma jednego z elementów jest równa argumentowi łańcucha i Ðfzachowuje tylko te elementy. jest tylko po to, aby umieścić wszystkie listy liczb pierwszych na tym samym poziomie na liście
dylnan
Teraz 10 bajtów , ponieważ i Ƈzostały dodane jako aliasy dla ЀiÐf
dylnan