New answers tagged primes
2
Factor + math.primes, sum 3373, 34 bytes
readln string>number prime? pprint
Try it online!
Should be pretty self-explanatory I hope!
1
Vyxal, 1 byte, sum 23
æ
Try it Online!
æ is the built-in primality checker. Vyxal uses its own codepage, in which the character æ is 0x17, or 23, which is a prime number.
1
Jelly, 9 bytes
Æv=¥ḷ#)Ṫ€
Try it online!
How it works
Æv=¥ḷ#)Ṫ€ - Main link. Takes n on the left
) - For each integer 1 ≤ k ≤ n, so the following:
¥ḷ# - Find the first k integers for which the following is true:
Æv - The number of distinct prime factors
= - equals k
Ṫ€ - Return the last element from each list
answered 13 hours ago
caird coinheringaahing
33k77 gold badges7272 silver badges218218 bronze badges
1
Jelly, 14 bytes
D<ƇḅṛɗⱮ⁵ÆfṀ€t0
Try it online!
How it works
D<ƇḅṛɗⱮ⁵ÆfṀ€t0 - Main link. Takes an integer n on the left
D - Cast n to digits, D
ɗ - Previous 3 links as a dyad f(D, k):
<Ƈ - Keep elements of D less than k
ṛ - Yield k
ḅ - Convert to base k
⁵ - 10
Ɱ ...
answered 13 hours ago
caird coinheringaahing
33k77 gold badges7272 silver badges218218 bronze badges
0
PPL 1.0.5, 85 bytes
fnp(n){
declareb=1
declaref=4<5
loop n - 2{
b=b+1
ifn%b<1{
f=3<2
}
}
unsetb
returnf
}
It is a "good practice" (defined by me, of course) to unset all unneeded variables before returning. The unset statement is like delete in JavaScript. The fn statement defines a function and returns the flag. Still many bugs to iron ...
0
Raku, 28 bytes
{first ($_~*).is-prime,1..*}
Try it online!
1
Excel, 34 bytes, sum 2039
=SUM(--(MOD(A2,SEQUENCE(A2))=0))=2
Uses SEQUENCE which was not available when the question was posted.
2
Ruby, 58 bytes
->n,r=1{(2...x=([n,r]*'').to_i).any?{|c|x%c<1}?f[n,r+1]:r}
Try it online!
1
Perl 5, 54 bytes
sub{$i=pop;$n=1;$n++while(1x"$i$n")=~/^(11+?)\1+$/;$n}
Try it online!
Perl 5, 65 bytes
A slightly different approach, but 11 bytes longer:
sub f{($i,$t)=@_;!$t||(grep"$i$t"%$_<1,2..$i.$t-1)?f($i,$t+1):$t}
Try it online!
1
Scratch, 320 bytes
Try it online!
Numbers that use values greater than 1 sextillion break because Scratch automatically converts them to scientific notation. Alternatively, 38 blocks.
when gf clicked
delete all of[P v
ask()and wait
set[N v]to(answer
repeat until<(length of[P v])=(1
repeat(length of[P v
set[N v]to(join(item(length of[P v])of[P v])(N
delete(...
0
Duocentehexaquinquagesimal, 23 bytes
7 ÇÇW∞DćƒΣZ;ndž“~3¦€`н¸k
Try it online!
0
Factor + math.primes math.unicode, 14 bytes
[ nprimes Σ ]
Try it online!
Explanation:
It's a quotation (anonymous function) that takes an integer as input and leaves an integer as output.
nprimes Obtain a list of the first n primes.
Σ Sum the list.
4
R, 64 62 61 60 bytes
function(n){while(sum(!(x=n*10^nchar(+F)+F)%%1:x)>2)F=F+1;F}
Try it online!
-1 byte thanks to @Dominic's idea and then another one (in both versions).
R, 60 59 bytes
function(n){while(sum(!(x=n*10^nchar(F)+F)%%1:x)>2)F=F+1;F}
Try it online!
Slower version from @Dominic.
Abuses the fact that n*10^5 will never be prime (but ...
1
Vyxal, 5 4 bytes, s
ræT›
Try it!
Explanation
r # Range a to b (input)
æ # Map prime check
T # Indices of truthy values
› # Increment
Then s flag sums the stack.
-1 thanks to @Lyxal
1
Vyxal, 2 bytes, s
ʁǎ
Try it!
Explanation
ʁ # Range from 0 to input
ǎ # Map to a_th prime
At last, sum with s flag
Disclaimer: my first Vyxal answer
3
Stax, 8 bytes
É3º╩(\╝░
Run and debug it
Explanation
wi;i\$e|p!
w while
|p! end result is not prime, do:
i push index
;i push input and index again
\$ stringify
e convert to int
final index is implicitly output
1
Retina, 53 bytes
.+
_;$&;_10**
+`((.+;).+);(__+)\3+$
_$1;$($1$.2)*
\G_
Try it online! Link includes less slower test cases. Explanation:
.+
_;$&;_10**
Create a working area with the suffix in unary (initially 1), the input prefix, and the current resulting integer in unary (initially one more than ten times the input).
((.+;).+);(__+)\3+$
Match ...
3
Brachylog, 5 bytes
;.cṗ>
Try it online!
,.ṗ> seems like it should work, but , seems to be somewhat bugged with unbound variables. It's a bit hard to tell why.
;.c The input's concatenation with the output
ṗ is a prime number
> greater than the output.
Uses > rather than ∧ so as to force the output to be an integer--otherwise, ...
1
Python 3.8, 80 bytes
f=lambda n,a=0:math.perm(p:=int(n+str(a)))**2%-~p<1and f(n,a+1)or-~a
import math
Try it online!
Inputs \$n\$ as a string and returns the smallest positive integer that makes a prime when appended to \$n\$.
2
Pyth, 6 bytes
fP_s+z
Test suite
Fairly different approach to the existing Pyth answer.
Explanation:
fP_s+z | Full code
fP_s+zT | with implicit variables
--------+-----------------------------------
f | first positive integer T such that
+zT | input and T concatenated
s | as an integer
P_ | is prime
3
Japt, 8 bytes
ÈsiU j}a
Try it
ÈsiU j}a :Implicit input of integer U
È :Function taking an integer as an argument
s : Convert to string
iU : Prepend U and convert back
j : Is prime?
} :End function
a :Get the first integer >=0 that returns true
2
J, 22 bytes
>:@]^:(0 p:,&.":)^:_&1
Try it online!
Increment counter >:@] while ^:_ original number catted with counter ,&.": is not prime 0 p:, with counter starting at 1 &1.
1
Julia 0.4, 39 bytes
with julia 0.4 for isprime and parse
>(x,n=1)=isprime(parse("$x$n"))?n:x>n+1
Try it online!
2
Python 3, 82 bytes
def x(n,i=1):k=int(f"{n}{i}");return all(k%j for j in range(2,k))and i or x(n,i+1)
Try it online!
-30 bytes thanks to @Manish Kundu, @MarcMush and @ophact
6
JavaScript (ES6), 51 50 bytes
Saved 1 byte thanks to @l4m2
Expects the input number as a string.
f=(n,k)=>eval('for(d=x=n+k;x%--d;)d')<3?k:f(n,-~k)
Try it online!
1
JavaScript, 65 bytes
f=(n,x=1)=>[...Array((i=n+x)-2)].every((_,e)=>i%(e+2))?x:f(n,x+1)
Feel like this is way too long for some reason. I'm not very good at primality testing, so that takes up the vast majority of my code. Basically checks if every number between 2 and n+[x] [the number with x appended at the end] yields a nonzero remainder when it ...
1
Pyth, 13 bytes
W!P_s+z=+Z1;Z
Try it online!
Explanation
Z # Set to 0 by default
z # Input, taken as a string
=Z+Z1 # Increment Z by 1 and assign Z to it
s+z # Concatenate z with and convert to integer
W!P_ # While it is not prime
;Z # Print Z after the while loop finishes
thanks Citty for -1 byte
4
05AB1E, 5 bytes
05AB1E treating strings and integers equal helps again.
∞.Δ«p
Try it online! or Try all cases
∞ # in the list of positive integers
.Δ # find the first one
« # that, when concatenated to the input,
p # is a prime number
1
Wolfram Language (Mathematica), 48 bytes
(k=0;While[!PrimeQ[10^⌊Log10@++k+1⌋#+k]];k)&
Increasing k until n*floor(10^(Log10(k)+1)+k) is prime
Try it online!
4
Husk, 9 8 bytes
Saved 1 byte thanks to Leo
ḟöṗr+⁰s1
Try it online!
Explanation:
ḟöṗr+⁰s
ḟ Find first number at least
1 1 such that
s when it's converted to a string,
+⁰ and appended to the input
r and read as an integer,
ṗ the result is prime
9
Jelly, 7 bytes
1ṭVẒɗ1#
Try it online!
How it works
1ṭVẒɗ1# - Main link. Takes n on the left
ɗ - Define a dyad f(k, n) from the previous 3 links:
ṭ - Tack; Yield [n, k]
V - Eval; Smash together into a single integer
Ẓ - Is prime?
1 1# - Starting from k = 1, find the first k such that f(k, n) is true
answered Apr 15 at 15:14
caird coinheringaahing
33k77 gold badges7272 silver badges218218 bronze badges
3
Japt, 11 bytes
j ?U:ßUk ¬n
j ? // If the input is prime
U // we're done, return the input.
: // Otherwise,
ß // recursively rerun
Uk // with the input's prime factors
¬n // joined together as a string and parsed to a number.
Try it here.
2
Pyth, 5 bytes
usjkP
Try it online!
Pyth could tie 05AB1E if it had a "join into integer" byte, or Ohm if it could take the primes factors of a string. Beats Jelly even without them, which surprises me.
3
Japt, 12 bytes
@=k ¬n)j}a;U
Try it
@...}a - first number to return a truthy value when passed trough:
@= > ignore input and assign 1st input U : *
k ¬n) * prime factors of U joined and converted to a number
j > return(is prime?)
;U - print U
3
R + numbers, 74 73 bytes
n=scan();while(F<-el(Reduce(paste0,numbers::primeFactors(n)):0)-n)n=n+F;n
Try it online!
1
PowerShell, 88 bytes
param($n)while($n-ne$m){$m=$n;$n=[int](((factor $m)-replace"^\d+: ").split()-join"")};$n
Try it online!
Well, I'm lazy, just ported @Noodle9
PowerShell, 203 bytes
param($k)$s='$f;$n/=$f';function F($n){$m=[math]::sqrt($n);$f=2;while(!($n%$f)){iex $s};$f=3;while($f-le$m-and$n-ge$m){while(!($n%$f)){iex $s};$f+=2};$n};...
5
Bash, 84 82 71 bytes
Saved 11 bytes thanks to caird coinheringaahing!!!
for((;$1-${2-0};)){ set - `factor $1|sed 's/.*://;s/ //g'` $1;};echo $1
Try it online!
Returns the home prime of \$n\$ quickly (performs all testcase in less than 2 seconds on TIO).
2
Python 3, 109 bytes
def f(n,m=0):
while n-m:
l=m=n;n=0
for i in range(2,l+1):
while l%i<1:n=int(f'{n}{i}');l/=i
return n
Try it online!
Quite verbose, returns the home prime of \$n\$.
4
Husk, 6 bytes
ω(rṁsp
Try it online!
ω( # iterate until reaching a fixed point:
p # get the prime factors
ṁs # convert each to a string & concatenate
r # convert the string to a value
4
Vyxal, 5 4 bytes
‡ǐṅẊ
Try it Online!
Jelly really do be getting rekt by stack languages though :p
This isn't 4 bytes because strings and integers aren't interchangeable like 05ab1e (and by extension Ohm), but that's okay. I added better type cohesion.
Explained
‡ǐṅẊ
‡ǐṅ # lambda x: "".join(prime_factorisation(x))
Ẋ # repeat the above on the ...
4
Haskell, 79 bytes
until((==)<*>g)g
g=read.f
f 1=""
f n=[show p++f(div n p)|p<-[2..],mod n p<1]!!0
Try it online!
The relevant function is until((==)<*>g)g, which takes as input a number n and returns its Home prime.
2
Retina 0.8.2, 37 bytes
{`.+
$*_
+`(__+?)(\1)*$
$.1_$#2$*_
_
Try it online! Somewhat slow, so link only includes faster test cases. Explanation:
{`
Repeat until the fixed point.
.+
$*_
Convert to unary.
+`
Repeat until all prime factors have been found.
(__+?)(\1)*$
Find the lowest factor of the remainder.
$.1_$#2$*_
Prefix the decimal of the factor to ...
2
Wolfram Language (Mathematica), 60 bytes
#//.x_:>FromDigits[ToString/@(""<>Table@@@FactorInteger@x)]&
Try it online!
-14 bytes from @att
6
APL(Dyalog Extended), 12 bytes SBCS
{⍎⊃,/⍕¨⍭⍵}⍣=
Try it on APLgolf!
A dfn submission which takes a single argument.
3
Jelly, 6 bytes
ÆfV$ÐL
Try it online!
This seems oddly long, but the prime factorize built-in is two bytes, I don't think there's a way to bypass needing the $, and the loop-until-not-unique built-in is only one byte long for the accumulator version.
8
JavaScript (ES6), 58 55 bytes
f=n=>n-(g=d=>q=n>1?n%d?g(d+1):d+g(d,n/=d):'')(2)?f(q):q
Try it online!
Commented
f = n => // f is a recursive function taking the input n
n - ( // subtract from n the result of a call to ...
g = d => // ... g: a recursive function taking a divisor d
q = ...
3
Ohm v2, 4 bytes
·ΘoJ
Try it online!
12
05AB1E, 3 bytes
ΔÒJ
Try it online!
Δ run until the output doesn't change:
Ò prime factors including duplicates
J join into an integer
3
Pyth, 14 bytes
W!P_Q=QsjkPQ;Q
Try it online!
Explanation
Q # integer input
W!P_Q # While Q is not prime
=Q # Set Q to
PQ # Returns prime factors of Q in increasing order.
k # Empty string
jk # Join them
s # Convert to integer
; # End of loop
Q # Print the final value of Q
2
Vyxal, 7 bytes
‡∆ṗεẊ1=
Try it Online!
Very nice and neat answer (flagless too!)
Explained
‡∆ṗεẊ1=
‡∆ṗε # lambda x: abs(x - prev_prime(x))
Ẋ # repeat that on the input until it doesn't change
1= # does that equal 1?
Top 50 recent answers are included
Related Tags
primes × 298code-golf × 257
math × 81
number-theory × 78
number × 57
sequence × 57
decision-problem × 29
code-challenge × 25
integer × 16
factoring × 16
restricted-source × 11
fastest-code × 11
arithmetic × 10
string × 8
kolmogorov-complexity × 8
fibonacci × 7
ascii-art × 6
binary × 6
base-conversion × 6
cops-and-robbers × 6
source-layout × 5
combinatorics × 4
date × 4
python × 4
palindrome × 4