| Bytes | Lang | Time | Link |
|---|---|---|---|
| 1075 | Vyxal | 241106T111845Z | lyxal |
| 014 | 05AB1E | 241106T105433Z | Kevin Cr |
| 021 | 05AB1E | 180111T112351Z | user2956 |
| 018 | Japt | 180110T180642Z | Shaggy |
| 016 | Jelly | 180110T175408Z | caird co |
| 122 | Python | 180112T231226Z | user8420 |
| 146 | Python 2 | 180110T183000Z | Jonathan |
| 016 | Husk | 180110T221913Z | ბიმო |
| 154 | Clean | 180110T214630Z | Οurous |
| 018 | 05AB1E | 180112T144643Z | Emigna |
| 128 | Python 3 | 180110T204603Z | 0WJYxW9F |
| 060 | J | 180111T012344Z | Bolce Bu |
| 120 | JavaScript ES6 | 180110T185950Z | Arnauld |
| 228 | PowerShell | 180110T184353Z | AdmBorkB |
| 011 | Pyth | 180110T174613Z | user4854 |
| 021 | Jelly | 180110T175516Z | Erik the |
| 025 | MATL | 180110T173931Z | Luis Men |
Vyxal, 86 bitsv2, 10.75 bytes
ẏuJ?vṀǐ@:gḟ
Bitstring:
10100100010000001010111000010111100110111011011010011001110010111000001110111001001010
05AB1E, 14 bytes
Ug©ÝΣ®‚£XýÒg}н
Try it online or verify (almost) all test cases.
Or alternatively:
ªε+Ns‚£¹ýÒg}Wk
Try it online or verify (almost) all test cases.
Inputs in the order \$B,A\$. Both programs have 0-based outputs.
Explanation:
U # Pop and store the first (implicit) input-integer in variable `X`
g # Pop and push the length of the second (implicit) input-integer
© # Store this length in variable `®` (without popping)
Ý # Pop and push a list in the range [0,length]
Σ # Sort it by:
®‚ # Pair the current index with length `®`
£ # Split the second (implicit) input into parts of those sizes
Xý # Join this pair with `X` as delimiter
Ò # Pop and push a list of its prime factors
g # Pop and push its length
}н # After the sort-by: pop and push the first/smallest one
# (which is output implicitly as result)
ª # Change the second (implicit) input-integer to a list of digits,
# and append the first (implicit) input-integer to this list
ε # Map over this list:
+ # Add this value to the second (implicit) input-integer
Ns‚ # Pair it with the current 0-based map-index as leading item
£ # Split the second (implicit) input into parts of those sizes
¹ý # Join this pair with the first input as delimiter
Òg # Same as above: amount of prime factors
}W # After the map: push the minimum (without popping)
k # Pop and get the (first) 0-based index of this minimum
# (which is output implicitly as result)
05AB1E, 27 21 bytes
ηõ¸ì¹.sRõ¸«)øεIýÒg}Wk
It returns the lowest 0-indexed p.
Thanks to @Emigna for -6 bytes !
Explanation
ηõ¸ì # Push the prefixes of A with a leading empty string -- [, 1, 12, 123, 1234]
¹.sRõ¸«) # Push the suffixes of A with a tailing empty space. -- [1234, 234, 34, 4, ]
ø # Zip the prefixes and suffixes
ε } # Map each pair with...
IýÒg # Push B, join prefix - B - suffix, map with number of primes
Wk # Push the index of the minimum p
Japt, 22 21 18 bytes
Takes the first input as a string and the second as either an integer or a string. Output is 0-indexed, returning all matching indices.
Êò@iXV n k Ê
ð@e¨X
Êò@iXV n k Ê\nð@e¨X :Implicit input of string U & integer (or string) V
Ê :Length of U
ò :Range [0,Ê]
@ :Map each X
iXV : Insert V in U at 0-based index X (if X=Ê, V gets appended to U)
n : Convert to integer
k : Prime factors
Ê : Length
\n :Reassign to U
ð :0-based indices that return true (Change to b to get the first index, or a to get the last)
@ :When passed through the following function as X
e : Is every element in U
¨X : Greater than or equal to U
Jelly, 16 bytes
;ṭJœṖ€$j€¥VÆfẈNM
Takes A as a list of digits and returns all 1-indices
How it works
;ṭJœṖ€$j€¥VÆfẈNM - Main link. Takes A on the left and B on the right
; - Concatenate B to the end of A
¥ - Group the previous 2 links into a dyad f(A, B):
$ - Group the previous 2 links into a monad g(A):
J - Indices of A; [1, 2, ..., len(A)]
€ - Over each index:
œṖ - Partition A at that index
€ - Over each partition:
j - Join with B
ṭ - Append the concatenated number to the end of this list
V - Evaluate each as a number
Æf - Calculate the prime factors of each
ẈN - Get the lengths of each and negate
M - Indices of maximal elements, which gives the minimal indices when negated
Python, 122 bytes
f=lambda n,i=2:n>1and(n%i and f(n,i+1)or 1+f(n/i,i))
g=lambda A,B:min(range(len(A)+1),key=lambda p:f(int(A[:p]+B+A[p:])))
In practice, this exceeds the default max recursion depth pretty quickly.
Python 2, 165 146 bytes
- Saved nineteen bytes thanks to Erik the Outgolfer.
O=lambda n:n>1and-~O(n/min(d for d in range(2,n+1)if n%d<1))
def f(A,B):L=[int(A[:j]+B+A[j:])for j in range(len(A)+1)];print L.index(min(L,key=O))
Husk, 16 bytes
§◄öLpr§·++⁰↑↓oΘŀ
Expects input as strings, try it online!
Explanation
§◄(öLpr§·++⁰↑↓)(Θŀ) -- input A implicit, B as ⁰ (example "1234" and "32")
§ ( )( ) -- apply A to the two functions and ..
(ö ) -- | "suppose we have an argument N" (eg. 2)
( § ) -- | fork A and ..
( ↑ ) -- | take N: "12"
( ↓) -- | drop N: "34"
( ·++⁰ ) -- | .. join the result by B: "123234"
( r ) -- | read: 123234
( p ) -- | prime factors: [2,3,19,23,47]
( L ) -- | length: 5
(öLpr§·++⁰↑↓) -- : function taking N and returning number of factors
in the constructed number
( ŀ) -- | range [1..length A]
(Θ ) -- | prepend 0
(Θŀ) -- : [0,1,2,3,4]
◄ -- .. using the generated function find the min over range
Clean, 165 ... 154 bytes
import StdEnv,StdLib
@n h#d=hd[i\\i<-[2..h]|h rem i<1]
|d<h= @(n+1)(h/d)=n
?a b=snd(hd(sort[(@0(toInt(a%(0,i-1)+++b+++a%(i,size a))),i)\\i<-[0..size a]]))
Python 3, 128 bytes
0-indexed; takes in strings as parameters. -6 bytes thanks to Jonathan Frech.
from sympy.ntheory import*
def f(n,m):a=[sum(factorint(int(n[:i]+m+n[i:])).values())for i in range(len(n)+1)];return a.index(min(a))
J, 60 Bytes
4 :'(i.<./)#@q:>".@(,(":y)&,)&.>/"1({.;}.)&(":x)"0 i.>:#":x'
Explicit dyad. Takes B on the right, A on the left.
0-indexed output.
Might be possible to improve by not using boxes.
Explanation:
4 :'(i.<./)#@q:>".@(,(":x)&,)&.>/"1({.;}.)&(":y)"0 i.>:#":y' | Whole program
4 :' ' | Define an explicit dyad
i.>:#":y | Integers from 0 to length of y
"0 | To each element
({.;}.)&(":y) | Split y at the given index (result is boxed)
(,(":x)&,)&.>/"1 | Put x inbetween, as a string
".@ | Evaluate
> | Unbox, makes list
#@q: | Number of prime factors of each
(i.>./) | Index of the minimum
JavaScript (ES6), 120 bytes
Takes input as 2 strings. Returns a 0-indexed position.
f=(a,b,i=0,m=a)=>a[i>>1]?f(a,b,i+1,eval('for(n=a.slice(0,i)+b+a.slice(i),x=k=2;k<n;n%k?k++:n/=x++&&k);x')<m?(r=i,x):m):r
Test cases
f=(a,b,i=0,m=a)=>a[i>>1]?f(a,b,i+1,eval('for(n=a.slice(0,i)+b+a.slice(i),x=k=2;k<n;n%k?k++:n/=x++&&k);x')<m?(r=i,x):m):r
console.log(f('1234','32')) // 1
console.log(f('3456','3')) // 4
console.log(f('378','1824')) // 0
console.log(f('1824','378')) // 4
console.log(f('67','267')) // 1
console.log(f('435','1')) // 1
PowerShell, 228 bytes
param($a,$b)function f($a){for($i=2;$a-gt1){if(!($a%$i)){$i;$a/=$i}else{$i++}}}
$p=@{};,"$b$a"+(0..($x=$a.length-2)|%{-join($a[0..$_++]+$b+$a[$_..($x+1)])})+"$a$b"|%{$p[$i++]=(f $_).count};($p.GetEnumerator()|sort value)[0].Name
(Seems long / golfing suggestions welcome. Also times out on TIO for the last test case, but the algorithm should work for that case without issue.)
PowerShell doesn't have any prime factorization built-ins, so this borrows code from my answer on Prime Factors Buddies. That's the first line's function declaration.
We take input $a,$b and then set $p to be an empty hashtable. Next we take the string $b$a, turn it into a singleton array with the comma-operator ,, and array-concatenate that with stuff. The stuff is a loop through $a, inserting $b at every point, finally array-concatenated with $a$b.
At this point, we have an array of $b inserted at every point in $a. We then send that array through a for loop |%{...}. Each iteration, we insert into our hashtable at position $i++ the .count of how many prime factors f that particular element $_ has.
Finally, we sort the hashtable based on values, take the 0th one thereof, and select its Name (i.e., the $i of the index). That's left on the pipeline and output is implicit.
Pyth, 20 13 11 bytes
.mlPsXbQzhl
Explanation
.mlPsXbQzhl
.m b Find the minimum value...
hl ... over the indices [0, ..., len(first input)]...
lP ... of the number of prime factors...
sX Qz ... of the second input inserted into the first.
MATL, 25 bytes
sh"2GX@q:&)1GwhhUYfn]v&X<
Inputs are strings in reverse order. Output is 1-based. If there is a tie the lowest position is output.
Try it online! Or verify all test cases.
Explanation
s % Implicitly input B as a string. Sum (of code points). Gives a number
h % Implicitly input A as a string. Concatenate. Gives a string of length
% N+1, where N is the length of A
" % For each (that is, do N+1 times)
2G % Push second input
X@ % Push 1-based iteration index
q % Subtract 1
: % Range from 1 to that. Gives [] in the first iteration, [1] in
% the second, ..., [1 2 ... N] in the last
&) % Two-output indexing. Gives a substring with the selected elements,
% and then a substring with the remaining elements
1G % Push first input
whh % Swap and concatenate twice. This builds the string with B inserted
% in A at position given by the iteration index minus 1
U % Convert to string
Yf % Prime factors
n % Number of elements
] % End
v % Concatenate stack vertically
&X< % 1-based index of minimum. Implicitly display