37
\$\begingroup\$

Input

no input for this challenge

The Challenge

Write a code that outputs:
The first 10 prime numbers whose sum of their digits equal the number of bytes of your code

Examples

Let's say your code is Co&%423@k"oo" which is 13 bytes
your code must output [67, 139, 157, 193, 229, 283, 337, 373, 409, 463]
those are the first 10 prime numbers whose sum.... ok you got it!

If your code is 8 bytes, you must output [17, 53, 71, 107, 233, 251, 431, 503, 521, 701]

Rules

You must only use the first 10^8 prime numbers
which means all your numbers must be <2038074743=10^8th prime

If you can't find 10 prime numbers in this range that fit your bytes, then you will have to adjust your code (you may even have to add some bytes!) in order to find a "working number of bytes"

Just output the 10 primes in any way you like

This is , so the shortest code in bytes win!

\$\endgroup\$
  • 1
    \$\begingroup\$ Can't edit comment so I'm just deleting and reposting: Here's a gist containing the primes you'd need to output for a given bytecount as well as their indices: link Since I did it naïvely, I only checked for the sums up to the first 10^7 primes. Like some answers have mentioned, there are missing values which likely correlate to unachievable digital sums, though for larger sums this may not be the case (remember, I only checked the first 10^7 primes). \$\endgroup\$ – cole Aug 23 '17 at 23:41
  • \$\begingroup\$ "in any way you like": do the primes need to be ordered? \$\endgroup\$ – Arnauld Aug 23 '17 at 23:45
  • \$\begingroup\$ @Arnauld No, but they must be the 10 smaller in any order \$\endgroup\$ – user73398 Aug 23 '17 at 23:47
  • 1
    \$\begingroup\$ @KevinCruijssen Although your answer is invalid you are welcome to post it in order to have a discussion with other members to help you find a better one. \$\endgroup\$ – user73398 Aug 24 '17 at 8:38
  • 1
    \$\begingroup\$ @TessellatingHeckler I also think the 10^8 cutoff is rather arbitrary. To verify that ten given numbers all have digit sum 350, and all are primes, should be fast. However, to verify that those ten are in fact the smallest possible, could be harder, I guess. \$\endgroup\$ – Jeppe Stig Nielsen Aug 25 '17 at 19:09

18 Answers 18

15
\$\begingroup\$

Neim, 10 8 7 bytes

-1 byte thanks to ASCII-only.

πᛦ𝐋Λ𝐬7𝔼

Try it online!

Outputs:

[7 43 61 151 223 241 313 331 421 601]

This (well, at least the approach) is ungolfable.

Explanation

πᛦ𝐋Λ𝐬7𝔼

π        push 13 (yes, very unintuitive :P)
 ᛦ       square (13² = 169)
  𝐋      push the first 169 primes
   Λ     filter those that when the following are executed, evaluate to 1
    𝐬    sum its digits and...
     7𝔼  check for equality against 7
\$\endgroup\$
  • 2
    \$\begingroup\$ best so far..!!! \$\endgroup\$ – user73398 Aug 24 '17 at 0:18
  • \$\begingroup\$ νᛦ𝐋Λ𝐬7𝔼 for 7... edit: π -> ν :p \$\endgroup\$ – Jonathan Allan Aug 24 '17 at 0:30
  • \$\begingroup\$ @JonathanAllan It seems you have been ninja'd by ASCII-only. ;) \$\endgroup\$ – totallyhuman Aug 24 '17 at 0:30
  • \$\begingroup\$ Wow there is really an instruction to push the first n primes? Neat! \$\endgroup\$ – isaac9A Aug 24 '17 at 22:56
9
\$\begingroup\$

Jelly, 14 bytes

‘ÆNDS=14ø⁵#‘ÆN

Try it online!

This prints:

[59, 149, 167, 239, 257, 293, 347, 383, 419, 491]

How it works

‘ÆNDS=14ø⁵#‘ÆN
          #     Count the first 
         ⁵        10
                Non-negative integers which meet:
‘ÆN               the (z+1)-th prime  (1-indexed :*()
   D              digits
    S             sum
     =            equals
      14          14
           ‘ÆN  Get those primes
\$\endgroup\$
  • 8
    \$\begingroup\$ get those primes! \$\endgroup\$ – undergroundmonorail Aug 23 '17 at 23:30
6
\$\begingroup\$

Pyth, 14 bytes

.f&qsjZT14P_ZT

Try it here.

This is 14 bytes and prints:

[59, 149, 167, 239, 257, 293, 347, 383, 419, 491]

Pyth, 16 bytes

.f&qssM`Z16P_ZTZ

Try it here!

Note that this could be 15 bytes: .f&qssM`Z16P_ZTZ, but there are no primes that have 15 as the sum of their digits, since 15 is divisible by 3, which would imply the number would also be divisible by 3, hence not being prime.

This is 16 bytes long and prints:

[79, 97, 277, 349, 367, 439, 457, 547, 619, 673]

How?

Explanation 1

.f&qsjZT16P_ZT - Full program.

.f&           T  - First 10 numbers that satisfy the following:

           P_Z     - Are prime and
    sjZT           - And their sum of digits
   q     14        - Equals 14.

Explanation 2

.f&qssM`Z16P_ZTZ - Full program.

.f&           T  - First 10 numbers that satisfy the following:

           P_Z     - Are prime and
    ssM`Z          - And their sum of digits
   q     16        - Equals 16.
\$\endgroup\$
  • 1
    \$\begingroup\$ Well done! and quick! \$\endgroup\$ – user73398 Aug 23 '17 at 22:57
  • \$\begingroup\$ “very few prime integers that have 15 as the sum of their digits” — indeed, because a digit sum divisible by 3 implies divisibility by 3 :) \$\endgroup\$ – Lynn Aug 23 '17 at 23:05
  • \$\begingroup\$ @Lynn Oh sure >_> - I'm dumb, sorry. Fixed \$\endgroup\$ – Mr. Xcoder Aug 23 '17 at 23:06
6
\$\begingroup\$

Husk, 13 bytes

↑10fȯ=13ΣdfṗN

Try it online!

Explanation

            N    Take the (infinite) list of all natural numbers.
          fṗ     Select only the primes.
   fȯ            Filter again by the result of composing the following three functions:
         d         Get the decimal digits.
        Σ          Sum them.
     =13           Check whether the sum equals 13.
↑10              Take the first 10 primes in the result.
\$\endgroup\$
6
\$\begingroup\$

Haskell, 77 71 bytes

x=take 10[i|i<-[1..],sum(read.pure<$>show i)==71,all((>0).rem i)[2..i-1]]

Try it online!

Saved 6 bytes thanks to Laikoni

For 71 bytes:

[89999999,99899999,99998999,99999989,189989999,189998999,189999989,197999999,199898999,199979999]

all your numbers must be <2038074743

1999999999 is the number with the maximum sum of digits in the allowed range, and that sum is 82. Any program that is beyond 82 bytes will not satisfy the condition. I hope that 77 bytes is ok, but I don't know (it's still running on my computer).

EDIT: a slightly optimized version gave for 77 bytes:

[699899999,779999999,788999999,789999989,797999999,798899999,799898999,799899899,799999799,879999899]
\$\endgroup\$
6
\$\begingroup\$

Jelly,  14  13 bytes

DS⁼13
.ȷÆRÇÐf

Try it online!

How?

DS⁼13 - Link 1, test digit sum equals thirteen: number, n
D     - get a list of the decimal digits of n
 S    - sum them
  ⁼13 - equals thirteen?

.ȷÆRÇÐf - Main link: no arguments
.ȷ      - literal 500
  ÆR    - prime range (all primes less than or equal to 500)
     Ðf - filter keep if:
    Ç   -   call last link (1) as a monad
\$\endgroup\$
5
\$\begingroup\$

Brachylog, 13 bytes

{.≜ṗẹ+13∧}ᶠ¹⁰

Try it online!

\$\endgroup\$
5
\$\begingroup\$

05AB1E, 10 bytes

83LØʒSOTQ}

Try it online!

The } is used as a filler, since 9 is an invalid byte count.

Output: [19, 37, 73, 109, 127, 163, 181, 271, 307, 433]

Explanation

83L        Push array [1, ..., 83], since 433 is the 83rd prime
   Ø       Map each to the nth prime
    ʒ      Get elements that return 1
     SO     When the sum of the digits
        Q   Equals
       T    10

Almost 8 bytes

This would be valid if one more byte could be golfed off.

žyLØʒSO8Q

Try it online!

Output: [17, 53, 71, 107, 233, 251, 431, 503, 521, 701]

Explanation

žy          Push number 128, since 701 is the 125th prime
  L         Push array [1, ..., 128]
   ØʒSO8Q   Map to nth primes and filter to those with a digit sum of 8 (see above)
\$\endgroup\$
3
\$\begingroup\$

Mathematica, 52 bytes

Select[Prime@Range[10^6],Tr@IntegerDigits@#==52&,10]

{799999,998989,999979,1789999,1798999,1979899,1989979,1997899,1999969,2599999}

thanx to @Not a tree for -6 bytes

\$\endgroup\$
  • 1
    \$\begingroup\$ You can tell Select how many things you want to return: Select[Prime@Range[10^6],Tr@IntegerDigits@#==52&,10] \$\endgroup\$ – Not a tree Aug 24 '17 at 3:11
  • 1
    \$\begingroup\$ Select[Prime@Range[77705],Tr@IntegerDigits@#==50&] \$\endgroup\$ – Lynn Aug 25 '17 at 17:17
2
\$\begingroup\$

J, 29 bytes

(#~(29=[:+/"."0@":)"0)p:i.872

Try it online!

Definitely works on the REPL, probably works a regular program too (not sure how J does output for programs to be honest).

First pass, not particularly ideal but I can't think of any more clever approaches. Going to investigate hardcoding a smaller sequence.

Explanation

(#~(29=[:+/"."0@":)"0)p:i.872
                      p:i.872  First 872 primes
 #~                            Filter by
   (29=[:+/"."0@":)"0            Digital sum = 29
                   "0              For each
                ":                 Convert to string
           "."0                    Convert each character to a number
         +/                        Sum results
    29=                            Equate to 29

872 guarantees that only the first 10 primes whose digital sum is 29 will be used.

\$\endgroup\$
2
\$\begingroup\$

V, 73 71 bytes

i8aa9
998a99
a98a
aa89
18998a9
18a8a
18a9989
197aa
199898a
1a7a9Ía/999

Try it online!

Simple substring replacement compression - I checked all the possible answer outputs, and then did some "which one has a simple string replacement that saves most characters" testing. e.g. generating this table. [edit: I looked again at the table and saw I could do the 71 byte version instead].

The higher primes have more long runs of 9 in them, and the best I found was where the digits add up to 73, the pattern 89999 -> 1 char brings the text down from 99 bytes to 63 bytes. Finding a way to undo 'a' -> '89999' in the remaining 10 bytes led me to V.

\$\endgroup\$
1
\$\begingroup\$

Japt, 19 bytes

L²õ f_j ©Zìx ¥19ïA

Test it


Explantaion

L²õ

Generate an array of integers (õ) from 1 to 100 (L) squared.

f_          Ã

Filter (f) by passing each through a function, where Z is the current element.

j

Check if Z is a prime.

©

Logical AND (&&).

Zìx

Split Z to an array of digits (ì) and reduce by addition (x).

¥19

Check for equality with 19.

¯A

Slice (¯) to the 10th element and implicitly output the resulting array.

\$\endgroup\$
1
\$\begingroup\$

Japt, 19 bytes

AÆ_j ©19¥Zì x «X´}a

Try it online! with the -Q flag to format the array.

Outputs the first 10 primes whose digits add to 19:

[199, 379, 397, 487, 577, 739, 757, 829, 883, 919]

Note that this could be golfed to 18 bytes (ì xìx), but no primes with a digit sum of 18 exist.

Explanation

Map the array [0, ..., 9] by the following function, where X is the current value.

  _              }a

Return the first integer that returns true from the following function, where Z is the current value

   j ©

Check if this number is prime, and...

      19¥Zì x

The sum (x) of the digits (ì) in Z equals (¥) 19,

              «X´

And X is falsy (« is "and not", or &&!). This also decrements X (´).

Resulting array is implicitly outputted.

\$\endgroup\$
  • \$\begingroup\$ Oh, nice; we tied with different approaches. \$\endgroup\$ – Shaggy Aug 23 '17 at 23:53
1
\$\begingroup\$

PARI/GP, 40 bytes

select(x->sumdigits(x)==40,primes(8600))

Not much golf about it (select those x with sum of digits 40 among first 8600 primes). Output is:

[49999, 68899, 69997, 77899, 78889, 78979, 79699, 79987, 85999, 88789]
\$\endgroup\$
1
\$\begingroup\$

Pyke, 10 bytes

~p#.cTq)T>

Try it here!

~p         -   primes
  #.cTq)   -  filter(^)
   .c      -    digit_sum
     Tq    -   ^ == 10
        T> - [:10]
\$\endgroup\$
1
\$\begingroup\$

Ruby 2.4.1, 74 Bytes

I was never going to beat one of the Codegolf optimised languages, but it was still fun to do it in Ruby. Frustrating that Prime is not in Core but in instead in the standard library. I am also frustrated that I can't beat Haskell.

2.4.1 :014 > require'prime';2.step.lazy.select{|n|n.digits.sum==74&&n.prime?}.first(10)
=> [389999999, 398999999, 498989999, 498998999, 499898999, 499899989, 499979999, 499989989, 579989999, 588999899]
\$\endgroup\$
0
\$\begingroup\$

Paradoc (v0.2.7+), 10 bytes (CP-1252)

5h¶fφTBŠT=

Try it online!

5h         .. 5 hundred
  ¶        .. Check if a number is prime...
   f       .. ...filter by the preceding block; converts 500
           .. to the range [0, 1, ..., 499]
    φ      .. Filter by the following block:
     T     .. Ten
      B    .. Base; convert to base-10 digits
       Š   .. Sum, resulting in the digit sum
        T  .. Ten
         = .. Check if (the digit sum and ten are) equal

Somewhat questionable since it prints all the numbers with no separator between them. An 11-byte program that prints each number on a separate line:

nIè¶fφTBŠE=

The only thing worth mentioning about it is the upper bound that's slightly more difficult to construct: is 18² = 324.

\$\endgroup\$
0
\$\begingroup\$

Bubblegum, 37 bytes

00000000: 0dc6 3901 4031 0c80 5043 9dfe 9180 7f63  ..9.@1..PC.....c
00000010: 6579 f028 9ed7 352d e7a3 4f48 37ff 9164  ey.(..5-..OH7..d
00000020: 4c96 04f7 02                             L....

Try it online!

Output is 29989,39799,39979,48799,48889,49789,56989,58699,58789,58897

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy