Prime Up your code

37

1

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!

user73398

Posted 2017-08-23T22:49:49.163

Reputation:

1

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).

– cole – 2017-08-23T23:41:15.333

"in any way you like": do the primes need to be ordered? – Arnauld – 2017-08-23T23:45:50.950

@Arnauld No, but they must be the 10 smaller in any order – None – 2017-08-23T23:47:39.023

@cole It's definitely impossible for all the missing numbers up through 69. There aren't 10 primes in the first 10^8 that sum up to 2, and there are no primes whose digits add up to any number divisible by 3 (except for 3 itself) since then that number would be divisible by 3. – Chris – 2017-08-24T04:34:38.927

"You must only use the first 10^8 prime number, which means all your numbers must be <2038074743=10^8th prime" I guess this invalidates my 364 bytes Java answer. xD Not that it returns 10 primes which digits are equal to 364 even if it has a year to run anyway.. – Kevin Cruijssen – 2017-08-24T08:32:45.083

1@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. – None – 2017-08-24T08:38:47.690

Why the arbitrary cutoff? "You must only use the first 10^8 prime numbers" effectively means "your program must be <=79 bytes" and that's not a normal codegolf restriction. What other purpose or reason is there for it? – TessellatingHeckler – 2017-08-24T23:45:55.200

@TessellatingHeckler do you think you can verify a 350 bytes answer? The task is very easy and can be done in a few bytes. I think the real challenge is to get to 5 or 4. If your language can't do this task in 80 bytes then you should try another language. or try hardcoding! – None – 2017-08-25T00:02:53.227

@TessellatingHeckler how about 1350 bytes then?Just a question: how many bytes is your answer? – None – 2017-08-25T00:40:58.393

1@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. – Jeppe Stig Nielsen – 2017-08-25T19:09:00.890

@BillSteihn I stopped trying because of the cutoff, so I don't know how long. I answered 71 bytes of V instead. – TessellatingHeckler – 2017-08-26T00:01:27.743

@JeppeStigNielsen Good point. With PrimeSieve generating primes, I wrote this Python script to sum the digits and find the first ten primes for each sum. The question's cutoff is 2 billion for 79 code characters. By 3 billion it's 80 chars. 4 billion, still 80. By 10 billion it's up to 86. By 20 billion, 88 chars. Here is the prime lists for up to 88 chars. clearer now that high sums will end up being 'remove repeating 9s'

– TessellatingHeckler – 2017-08-26T00:08:09.203

For the record, up to 100 billion gets to 95 chars - https://gist.github.com/anonymous/cb500b6a7dfc114df89e54941797ab0b

– TessellatingHeckler – 2017-08-29T17:27:17.053

Answers

15

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

totallyhuman

Posted 2017-08-23T22:49:49.163

Reputation: 15 378

2best so far..!!! – None – 2017-08-24T00:18:41.127

νᛦΛ7 for 7... edit: π -> ν :p – Jonathan Allan – 2017-08-24T00:30:04.227

@JonathanAllan It seems you have been ninja'd by ASCII-only. ;) – totallyhuman – 2017-08-24T00:30:31.847

Wow there is really an instruction to push the first n primes? Neat! – isaac9A – 2017-08-24T22:56:08.227

9

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

fireflame241

Posted 2017-08-23T22:49:49.163

Reputation: 7 021

8get those primes! – undergroundmonorail – 2017-08-23T23:30:20.353

6

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.

Mr. Xcoder

Posted 2017-08-23T22:49:49.163

Reputation: 39 774

1Well done! and quick! – None – 2017-08-23T22:57:42.193

“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 :) – Lynn – 2017-08-23T23:05:44.350

@Lynn Oh sure >_> - I'm dumb, sorry. Fixed – Mr. Xcoder – 2017-08-23T23:06:23.707

6

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

Jonathan Allan

Posted 2017-08-23T22:49:49.163

Reputation: 67 804

6

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.

Martin Ender

Posted 2017-08-23T22:49:49.163

Reputation: 184 808

Almost golfed it to 10 bytes, but one of the results is wrong :( https://tio.run/##yygtzv7//1HbREODtPxDyywftU04sqHg/38A

– Leo – 2017-08-24T10:31:50.670

@Leo Heh, would be very neat if that worked :) – Martin Ender – 2017-08-24T11:01:23.997

6

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]

jferard

Posted 2017-08-23T22:49:49.163

Reputation: 1 764

5

Brachylog, 13 bytes

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

Try it online!

Leaky Nun

Posted 2017-08-23T22:49:49.163

Reputation: 45 011

5

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)

Justin Mariner

Posted 2017-08-23T22:49:49.163

Reputation: 4 746

3

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

J42161217

Posted 2017-08-23T22:49:49.163

Reputation: 15 931

1You can tell Select how many things you want to return: Select[Prime@Range[10^6],Tr@IntegerDigits@#==52&,10] – Not a tree – 2017-08-24T03:11:14.060

1Select[Prime@Range[77705],Tr@IntegerDigits@#==50&] – Lynn – 2017-08-25T17:17:44.823

2

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.

cole

Posted 2017-08-23T22:49:49.163

Reputation: 3 526

2

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.

TessellatingHeckler

Posted 2017-08-23T22:49:49.163

Reputation: 2 412

1

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.

Justin Mariner

Posted 2017-08-23T22:49:49.163

Reputation: 4 746

Oh, nice; we tied with different approaches. – Shaggy – 2017-08-23T23:53:23.540

1

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.

Shaggy

Posted 2017-08-23T22:49:49.163

Reputation: 24 623

1

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]

Jeppe Stig Nielsen

Posted 2017-08-23T22:49:49.163

Reputation: 602

1

Pyke, 10 bytes

~p#.cTq)T>

Try it here!

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

Blue

Posted 2017-08-23T22:49:49.163

Reputation: 26 661

1

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]

cpt_peter

Posted 2017-08-23T22:49:49.163

Reputation: 21

0

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.

betaveros

Posted 2017-08-23T22:49:49.163

Reputation: 741

0

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

ovs

Posted 2017-08-23T22:49:49.163

Reputation: 21 408