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
Nappearing (for instance, the17in1175321), but rather for the first instance ofNas 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 code-golf, so the author of the code with the least length in bytes wins!
11 Answers 11
Jelly, 9 bytes
ÆR1;;\UFV
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.
-
\$\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
Von a list.) \$\endgroup\$user62131– user621312016年12月21日 00:15:01 +00:00Commented Dec 21, 2016 at 0:15
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);
}
}
Jelly, 12 bytes
ÆR;@1
ÇÇ€UVV
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.
05AB1E, 19 bytes
LDpÏX ̧ì€Lí ̃ÐXQsp+ÏJ
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
-
\$\begingroup\$ Amazed by the
DpÏstatement. Nice work! \$\endgroup\$devRicher– devRicher2016年12月20日 14:35:01 +00:00Commented Dec 20, 2016 at 14:35
Brachylog, 17 bytes
y:{e1|e#p}f@[rcw\
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
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
-
\$\begingroup\$ Nice, someone who uses GML \$\endgroup\$kepe– kepe2018年10月05日 16:41:24 +00:00Commented 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\$Timtech– Timtech2018年10月07日 13:41:47 +00:00Commented Oct 7, 2018 at 13:41
MATL, 15 bytes
Zq"1@ZqP]1v!VXz
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
Perl 6, 41 bytes
{[~] flat [\R,] 1,|grep *.is-prime,2..$_}
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.)
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.
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)
1is a prime by definition. \$\endgroup\$1directly 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\$