12
\$\begingroup\$

Im talking about this question, take a look at it if you are a bit confused.

Main Task

Your task is to output concatenated integers, in decreasing order, but increasing the maximum integer everytime you hit 1 (for this question, 1 will be considered a prime number). While this doesn't sound any different from the first question, here comes the tricky part: All outputted numbers may only be primes. These will be strung together into a single string without spaces or newlines. Your input will also be a prime number.

Example:

1
21
321
5321
75321
1175321
Valid output:
1213215321753211175321

Input

Your code may only take one input: the highest prime to be printed. This input can come from anywhere (graphical, STDIN). You are assured that the input is a prime number.

Output

You will have to output the resulting number. You can get this number by keep counting down, only count the number if it's a prime, then connect all results together to one number. The last number "row" (e.g. 7, 5, 3, 2, 1) has to be printed fully. The output could be anything (numbers, strings, graphical), as long as it's readable. The same Regex pattern for checking your test cases applies:

^(\D*(\d)+\D*|)$

If your output doesn't match with this pattern, your code is invalid.

Rules

  • The input is assured to be prime, do not include error handling, unless you want/need to.
  • The output may only be a full-connected number, therefore not split up by anything, not even newlines.
  • Your algorithm shouldn't check for the first instance of N appearing (for instance, the 17 in 1175321), but rather for the first instance of N as the actual number.
  • Your input is assured to be positive, do not add handling unless you want/need to.

Test cases

Input: -2, 0
Output: Any, or none (number isn't positive)
Input: 9
Output: Any, or none (number isn't prime)
Input: 1
Output: 1
Input: 7
Output: 121321532175321
Input: 23
Output: 1213215321753211175321131175321171311753211917131175321231917131175321

Winner

This is , so the author of the code with the least length in bytes wins!

asked Dec 20, 2016 at 12:47
\$\endgroup\$
10
  • 8
    \$\begingroup\$ I won't suggest changing the challenge, but I don't think 1 is a prime by definition. \$\endgroup\$ Commented Dec 20, 2016 at 14:28
  • 3
    \$\begingroup\$ 1. Having a test case 1 directly contradicts the spec, which "assures" that the input number will be a prime. 2. The output spec seems to contain multiple contradictions and ambiguities. "The last number "row" (e.g. 7, 5, 3, 2, 1) has to be printed fully" - so the others don't? "The same Regex pattern for checking your test cases applies", but "The output may only be a full-connected number, therefore not split up by anything" contradicts that regex. But the regex is clearly dodgy anyway because it allows the empty string, and there's no input which could give that. \$\endgroup\$ Commented Dec 20, 2016 at 14:41
  • 1
    \$\begingroup\$ 1. "A single trailing newline is allowed." is redundant/inconsistent with the regex pattern that allows any number of trailing characters. 2. The intro sentence "Your task is to output integers" is misleading since you later ask to output a single number. 3. The whole explanation of the sequence and the output is confusing - people basically have to reverse-engineer what you mean by studying the examples (sequence listing and test cases). The last challenge also had these issues, and I addressed them there in a suggested edit there, which you rejected... :( \$\endgroup\$ Commented Dec 20, 2016 at 16:15
  • 5
    \$\begingroup\$ What's the point in arbitrarily making 1 a prime? \$\endgroup\$ Commented Dec 20, 2016 at 16:17
  • 1
    \$\begingroup\$ The negative order integer challenge but every time it's a prime it gets faster ;) \$\endgroup\$ Commented Dec 21, 2016 at 6:17

11 Answers 11

5
\$\begingroup\$

Jelly, 9 bytes

ÆR1;;\UFV

Try it online!

How it works

ÆR1;;\UFV Main link. Argument: n
ÆR Prime range; yield all primes up to n.
 1; Prepend the "prime" 1.
 ;\ Cumulative concatenation; yield all prefixes of the prime range.
 U Upend; reverse each prefix.
 F Flatten the resulting 2D array.
 V Eval. This casts the integer array to string first, thus concatenating
 the integers.
answered Dec 20, 2016 at 17:26
\$\endgroup\$
1
  • \$\begingroup\$ I know I'm too deep into learning about golfing when I read the Jelly to understand the question rather than the other way round. (This is actually a pretty readable Jelly program, as they go; the only confusing point for me was the weird special case of V on a list.) \$\endgroup\$ Commented Dec 21, 2016 at 0:15
5
\$\begingroup\$

Processing, 161 bytes

int p(int n){for(int k=1;++k<=sqrt(n);)if(n%k<1)return 0;return 1;}void t(int n){for(int i=1,j;i<=n;i++){if(p(i)<1)continue;for(j=i;j>0;j--)print(p(j)<1?"":j);}}

One function does the primality checking, the other does the printing. Call it by t(7)

Ungolfed

The first function does the primality checking. It returns an int instead of a boolean since this way more bytes are saved. (int instead of boolean, 0 instead of false, 1 instead of true)

int Q103891p(int n){
 for(int k=1;++k<=sqrt(n);)
 if(n%k<1)return 0;
 return 1;
}

The second function prints out the string. It iterates through every number, if it is not a prime, skip to the next iteration. If it is a prime, it continues down to the printing inside another for-loop. Again, if the number is prime, then we print it, otherwise not.

void Q103891(int n){
 for(int i=1,j;i<=n;i++){
 if(p(i)<1)continue;
 for(j=i;j>0;j--)
 print(p(j)<1?"":j);
 }
}
answered Dec 20, 2016 at 13:23
\$\endgroup\$
5
\$\begingroup\$

Jelly, 12 bytes

ÆR;@1
ÇÇ€UVV

Try it online!

If it hadn't been for 1s at all, my code would have just been ÆRÆRUVV for 7 bytes.

Enhanced explanation:

ÇÇ€UVV Main link. Arguments: z.
Ç Run link1 on z.
 ǀ Run link1 on z's elements.
 U Reverse z's elements.
 V Flatten z.
 V Concatenate z's elements.
ÆR;@1 Link 1. Arguments: z.
ÆR Range of primes [2..z].
 1 Integer: 1.
 ;@ Concatenate x to y.

The Irish guy (called Dennis?) somehow outgolfed me lol.

answered Dec 20, 2016 at 14:39
\$\endgroup\$
0
4
\$\begingroup\$

05AB1E, 19 bytes

LDpÏX ̧ì€Lí ̃ÐXQsp+ÏJ

Try it online!

Explanation

L # range [1 ... input]
 DpÏ # keep only primes
 X ̧ì # prepend a 1
 €L # map: range [1 ... n]
 í # reverse each sublist
 ̃ # flatten list to 1D
 Ð # triplicate
 XQ # check elements in one copy for equality with 1
 sp # check elements in one copy for primality
 + # add the above lists giving a list with true values at indices
 # comtaining 1 or a prime
 Ï # keep only those elements of the unmodified copy of the list
 J # join
answered Dec 20, 2016 at 13:32
\$\endgroup\$
1
  • \$\begingroup\$ Amazed by the DpÏ statement. Nice work! \$\endgroup\$ Commented Dec 20, 2016 at 14:35
2
\$\begingroup\$

Brachylog, 17 bytes

y:{e1|e#p}f@[rcw\

Try it online!

Can't seem to get shorter than that...

Explanation

y The list [0, ..., Input]
 :{ }f Find all...
 e1 ...elements that are 1 (there is 1)...
 | ...or...
 e#p ...elements that are prime...
 @[ Take a prefix of the result
 rc Reverse it and concatenate it into a number
 w Write to STDOUT
 \ Backtrack: try another prefix
answered Dec 20, 2016 at 16:04
\$\endgroup\$
2
\$\begingroup\$

GameMaker Language, 169 bytes

Main function (68 bytes)

b=""for(i=1;i<=argument0;i++){c=i while(j)b+=string(p(c--))}return b

Function p (46 bytes)

for(a=0;a<argument0;a++)while q(++b){}return b

Function q (55 bytes)

n=argument0 for(i=2;i<n;i++)if!(n mod i)p=1return p|n=1
answered Dec 20, 2016 at 16:18
\$\endgroup\$
2
  • \$\begingroup\$ Nice, someone who uses GML \$\endgroup\$ Commented Oct 5, 2018 at 16:41
  • \$\begingroup\$ @FireCubez Thanks:) I used to use it a lot. It was actually the first programming language I learned. \$\endgroup\$ Commented Oct 7, 2018 at 13:41
1
\$\begingroup\$

MATL, 15 bytes

Zq"1@ZqP]1v!VXz

Try it online!

Zq % Implicit input. Push array of primes up to that value
" % For each prime in that array
 1 % Push 1
 @ % Push current prime
 Zq % Push array of primes up to that
 P % Reverse
] % End
1 % Push 1
&h % Concatenate all stack horizontally
V % Convert to string
Xz % Remove spaces. Implicit display
answered Dec 20, 2016 at 14:10
\$\endgroup\$
1
\$\begingroup\$

Perl 6, 41 bytes

{[~] flat [\R,] 1,|grep *.is-prime,2..$_}

(Try it online.)

Explanation:

  • 1, |grep(*.is-prime, 2..$_): Sequence of 1 and primes... (1 2 3 5)
  • [,] ...: Reduce ("fold") over comma operator... (1 2 3 5)
  • [,円] ...: With intermediate results (triangular reduce)... ((1) (1 2) (1 2 3) (1 2 3 5))
  • [\R,] ...: Apply reversing meta-operator to the comma... ((1) (2 1) (3 2 1) (5 3 2 1))
  • [~] flat ...: Remove list nesting, and fold over string concat operator... 1213215321

(This is based on my answer for the previous challenge.)

answered Dec 20, 2016 at 16:36
\$\endgroup\$
1
\$\begingroup\$

Mathematica, 61 bytes

ToString/@(1<>Prime@Range[Range@PrimePi@#,0,-1]/.Prime@0->1)&

Unnamed function taking an integer argument and returning a string. (If the input is not a prime, it just "rounds it down" to the nearest prime; if the input is nonpositive, it pretends it's 1.)

This implementation uses the nasty trick from Martin Ender's answer to the similar previous challenge (who says this old dog can't learn new tricks?): abusing <> to flatten a nested list of integers.

The nested list in question starts by generating a similar nested list as in that answer, with the appropriate length (given by PrimePi@#, the number of primes up to and including the input); then Prime is applied to every element. For example, for the input 5 which is the 3rd prime, the code Range[Range@PrimePi@#,0,-1] yields {{1,0},{2,1,0},{3,2,1,0}}, and applying Prime to each element yields {{2,Prime[0]},{3,2,Prime[0]},{5,3,2,Prime[0]}} since the 1st, 2nd, and 3rd primes are 2, 3, and 5, respectively. I feel proud that I managed to add even more errors to Martin Ender's approach—Mathematica complains every time it writes Prime[0].

Prime[0] isn't a thing, but that's okay: /.Prime@0->1 turns them all into 1s. And we also want a 1 on the front, so we replace the "" from Martin Ender's answer with simply 1, which actually saves a byte.

answered Dec 20, 2016 at 19:08
\$\endgroup\$
0
\$\begingroup\$

PHP, 72 bytes

for(;$n<$argv[1];print$s=$n.$s)for($i=2;$i>1;)for($i=++$n;--$i&&$n%$i;);

Run wit -r

breakdown

for(;$n<$argv[1]; // loop $n up to argument:
 print$s=$n.$s) // 2. prepend $n to $s, print $s
 for($i=2;$i>1;) // 1. find next prime: break if $i<2
 for($i=++$n;--$i&&$n%$i;); // if $n is prime, $i is 1 after loop (0 for $n=1)
answered Dec 20, 2016 at 18:14
\$\endgroup\$
0
\$\begingroup\$

Pyth - 12 bytes

jkf!tPTs}R1S

Test Suite.

answered Dec 21, 2016 at 1:07
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.