New answers tagged

0

ink, 50 bytes ==function f(n) { -!n:~return 1 } ~return n*f(n-1) Try it online! Had to make it a proper function instead of just a stitch, since I actually have to use the return value (so outputting by printing is not an option - or at least not a good one).


1

Rockstar, 129 bytes listen to N listen to R X's0 O's0 while N-X let X be+1 P's1 Y's0 while X-Y let P be*R-0 let Y be+1 let O be+P+""+P*P is N say O Try it here (Code will need to be pasted in, with n on the first line of input and r on the second)


3

Rust, 72 70 bytes |n,r|(0..n).any(|i|format!("{}{}",r.pow(i),r.pow(2*i))==n.to_string()) Try it online! A port of ovs's 05AB1E answer. Thanks to ovs for helping save 2 bytes!


0

sed, 5 bytes /0$/d Try it online! Takes the input as a binary string. sed does not have truthy/falsey values, so I am defining truthy as a non-empty output and falsey as an empty output. As per this meta post, this is acceptable since these sets are non-overlapping. For the cost of one byte, we can instead output by exit code: /0$/q1 The following also ...


-1

05AB1E, 1 Byte É Try it online! # (implicit) push STDIN to stack É # push 1 if top of stack is odd, 0 if even # (implicit) output top of stack to STDOUT


3

PHP, 63 bytes function($n,$r){while($n>$b=($a=$r**$x++).$a*$a);return$n==$b;} Try it online! Or... put another way... PHP, 63 bytes function($n,$r){while(0<$b=$n<=>($a=$r**$x++).$a*$a);return$b;} Try it online! Can't seem to get away from this number... PHP, 63 bytes function($n,$r){while($n>$a=$r**$x.$r**($x++*2));return$n==$a;} Try it ...


3

Wolfram Language (Mathematica), 52 bytes #^2+10^IntegerLength[#^2]#&[#2^0~Range~#]~MemberQ~#& Try it online!


2

Jelly, 9 bytes Uses the evaluation (V) trick from Unrelated String's answer - go give an upvote! *⁹ŻżḤ$¤Vċ A dyadic Link accepting an integer \$r>1\$ on the left and an integer \$n>0\$ on the right which yields 1 if \$n\$ can be expressed as the concatenation of a power of \$r\$ and its square, or 0 if not. Try it online! Or see the test-suite (large \...


2

Jelly, 10 9 bytes Ḷ*@ż²$Vi⁸ Try it online! -1 thanks to Jonathan Allan Elided the two larger test cases for the sake of being able to run. Adapted from my own answer to the CMC. I've also attempted to adapt one of HyperNeutrino's cleverer answers, but it comes out to the same length on account of needing Ḷ to handle the [11, r]: Jelly, 10 9 bytes ḶżḤ$*@Vi⁸ ...


0

Rockstar, 47 bytes listen to N let D be N/2 turn up D say D is N/2 Try it here (Code will need to be pasted in)


6

R + pryr, 43 39 bytes Edit: -4 bytes thanks to pajonk Or R, 43 bytes pryr::f(any(n==paste0(s<-r^(0:n),s^2))) Try it online! A nice function that is naturally short thanks to R's vectorization. s<-r^(0:n) generates a vector of all powers-of-r from 0..n (the <- here is an R assignment operator, similar to =), paste0(s,s^2) generates a character ...


2

MathGolf, 11 bytes r#mÆ‼░²░+l╧ Try it online. (The two test cases with the largest \$n\$ are timing out.) Explanation r # Push a list in the range [0, (implicit) input `n`) # # Take (implicit) input `r` to the power of each value in this list m # Map over this list, Æ # Using the following five commands: ‼ ...


2

SNOBOL4 (CSNOBOL4), 96 bytes N =INPUT R =INPUT N Z =R ^ X Y =EQ(N,Z Z ^ 2) 1 :S(O) X =LE(Z,N) X + 1 :S(N) O OUTPUT =Y END Try it online! Prints 1 for Truthy, and an empty line for Falsey. N =INPUT ;* Input n R =INPUT ;* input R N Z =R ^ X ;* set Z = R^X (X starts as "" or 0) Y =EQ(N,...


3

Japt -x, 11 bytes ÆVpXã¥X+²s Try it ÆVpXã¥X+²s :Implicit input of integers U=n and V=r Æ :Map each X in the range [0,U) VpX : Raise V to the power of X à :End map £ :Map each X ¥ : Test U for equality with X+ : X appended with ² : X squared s ...


6

Brachylog, 9 bytes Takes r as input and n as output. Unifies if truthy, otherwise fails. ;A^gj^₂ᵗc Try it online! How it works ;A^gj^₂ᵗc with implicit r as input ;A^ r^some number gj [r^some number, r^some number] ^₂ᵗ [r^some number, r^some number^2] c concatenated is the output n


8

Python 3, 63 \$\cdots\$ 56 54 bytes Saved 4 bytes thanks to ovs!!! Saved a byte porting Arnauld's golf of Shaggy's JavaScript answer!!! Saved 2 bytes thanks to pxeger!!! f=lambda n,r,p=1:p>n or(n-int(f'{p}{p*p}'))*f(n,r,r*p) Try it online! Returns a falsey if \$n\$ can be expressed as the concatenation of a power of \$r\$ and its square or truthy ...


4

JavaScript, 47 44 38 bytes n=>g=(r,x=1)=>x<n&&[x]+x*x==n|g(r,x*r) -6 bytes thanks to Arnauld. Try it online!


8

APL (Dyalog Unicode), 21 bytes (SBCS) Anonymous infix lambda, taking \$r\$ as left argument and \$n\$ as right argument. Requires ⎕IO←0 (zero-based indexing). {⍵∊(⊢⍎⍤,⍥⍕¨×⍨)⍺*⍳⌊⍟⍵} Try it online! (Dyalog Extended as polyfill for version 18.0) {…} "dfn", ⍺ is \$r\$ and ⍵ is \$n\$:  ⍟⍵ natural log of \$n\$ (to avoid overflow)  ⌊ round that down  ⍳ ...


8

05AB1E, 9 bytes ÝmεDn«}¹å Try it online! This is a little inefficient, so don't try the larger falsey test cases. Commented: # implicit input, n first, r second Ý # inclusive range from 0 to n m # raise r to all of these powers ε } # map over the powers ... D # duplicate power n # square it « ...


3

Javascript (V8), 63 60 59 53 51 43 bytes -3 from Neil -2 and -8 from Shaggy n=>r=>[...n+n].some((_,i)=>[p=r**i]+p*p==n) Takes input via currying: f("16256")(2). Works quickly and for all values within the safe integer limit (\$2^{52}-1\$). Returns true or false. Old n=>r=>[...n+n].map((a,i)=>[s=r**i]+s*s).indexOf(n) n=>r=>[...


2

Spaghetti, 522 bytes main:0"n"goto store goto l l:100"n"goto retrieve goto areEqual"EOF"goto jumpIfTrue"n"goto retrieve 1 2 goto add"n"goto store 15"n"goto retrieve 2 goto modulus 0 goto areEqual"f"goto jumpIfTrue 3"n"goto retrieve 2 goto modulus 0 goto areNotEqual"b"...


1

APL (NARS2000), 11 chars, 20 bytes {×/(π⍺)∩π⍵} Examples: 28{×/(π⍺)∩π⍵}144 4 69{×/(π⍺)∩π⍵}25 1 Why it works: Function π, when applied monadically, breaks down argument into prime factors. (π⍺)∩π⍵ gives intersection of prime factors of left and right argument. ×/ multiplies prime factors in the intersection, giving the largest divisor common to ⍺ ...


0

Ral, 45 37 bytes ,:,+0=0*/-:1+1:+1+:+:1=?0*+0*/:0=1*?. Try it online! (inputs on separate lines) Commented: ,:, Input a and b +0= Add a to b Loop: 0*/- Subtract b from a :1+1:+1+:+:1=? Continue if a >= 0 0*+ Add b to a 0*/:0= Swap a and b 1*? ...


1

Japt -h, 5 bytes Input as an array. mâ rf Try it mâ rf :Implicit input of integer array m :Map â : Divisors r :Reduce by f : Filtering, keeping only those elements in the first array that also appear in the second :Implicit output of last element


2

ARMv7 (OakSim), 28 bytes Hexdump: 0x00010000: 01 00 80 E0 00 10 81 E0 01 00 50 E1 01 00 40 C0 ..........P...@. 0x00010010: 00 10 41 B0 FB FF FF 1A 1E FF 2F E1 00 00 00 00 ..A......./..... Explanation Callable function, expects the arguments in r0 and r1, output is in r0. Expects address of caller stored in lr. This is the standard method of procedure ...


2

Add++, 89 bytes l:100 Fl,`f,i%3,f=0,"Fizz"*f,`b,i%5,b=0,"Buzz"*b,`p,f+b,p="",Ip,,Oi,`c,f+b,`P,p=0,IP,,Oc, Try it online! "But there's a shorter Add++ answer" I hear you say. Well, this is flagless and doesn't have a Lambda, so I'd thought I'd post it for comparison.


-1

Python, 13 bytes eval(input())


1

Java 7, 60 42 bytes 0 Indexed int s(int n){return n<1?1:3*s(n/2)-2+n%2;} Using the implicit sequence definition from OEIS. Thanks to Kevin for the improvement using only one return statement.


3

05AB1E, 8 bytes LÕO·<sn/ Try it online! Explanation: L Push inclusive range of (implicit) input (4 -> [1, 2, 3, 4]) Õ Perform Euler's Totient on each element ([1, 2, 3, 4] -> [1, 1, 2, 2]) O Sum list elements ([1, 1, 2, 2] -> 6) ·< Double result and subtract 1 (2*6 - 1 = 11) sn Square input (4 -> 16) ...


2

Python 3, 53 bytes s=lambda p,q:p+q and 10*s(p//10,q//10)+max(p%10,q%10)


0

Japt, 4 bytes 0-indexed Ò¢n3 Try it or run all test cases from the OEIS entry Same as most other solutions: Ò¢n3 :Implicit input of integer U Ò :Negation the bitwise NOT of ¢ :U converted to a binary string n3 :Converted from a ternary string


0

Pip, 11 bytes 1+(aTB2)FB3 Try it online! Based on Dennis' Jelly answer. Zero Indexed.


1

APL (Dyalog Extended), 5 bytes 1+3⊥⊤ Try it online! Jo King's suggestion. APL (Dyalog Unicode), 12 bytes 1+3⊥2(⊥⍣¯1)⊢ Try it online! Based on Dennis' Jelly answer. Outputs are zero indexed. 1+3⊥2(⊥⍣¯1)⊢ ⊢ Take argument 2(⊥⍣¯1) Encode in binary 3⊥ Decode from ternary 1+ Add 1


0

Rockstar, 71 62 bytes listen to N X's0 P's1 while N-X let X be+1 let P be*N-0 say P Try it here (Code will need to be pasted in)


1

GolfScript, 9 bytes 1\~,{)*}/ Try it online! 1\ # Puts 1 under the input, this will be the acumulator ~, # Makes an array with numbers from 0 to (n-1) {)*} # This block goes to the top of the stack without being executed, when executed it increments and multiplies, this avoids multiplying by 0 and also multiplies by n / # ...


2

APL (Dyalog Extended), 131 bytes ⌷∘⎕D⍤0{S←{⍵↓⍨(⊥⍨-∧/)⌽0=⍵}⋄L←<⍥≢∨=⍥≢∧{>/⍋⍵⍺}⋄⍺L⍵:0⍺⋄q a←(⍵∇⍨¯1↓⍺),¨0,⊢/⍺⋄S¨q(a{⍺L⍵:⍺⋄(⊃⌽q)+←1⋄(S(+⌿1 0⌽0 10⊤⊢)⍣≡⍺-⍵↑⍨-≢⍺)∇⍵}⍵)}⍥(⍎¨) Try it online! An anonymous function that takes n (dividend) on the left and m (divisor) on the right, and returns a 2-row char matrix of digits where the first row is the quotient and ...


0

Husk, 16 15 bytes ËΣCẊ-mi¡+/3L¹0d Try it online! Returns 4 for truthy and 0 for falsy. Explanation ËΣCẊ-mi¡+/3L¹0d Input is a number, say n=34725. L¹ Length of n (number of digits): 5 /3 Divide by 3 (gives a rational number): 5/3 ¡+ Iterate addition of 5/3 0 starting from 0: [0,5/3,10/3,5,20/3,...


0

Japt -x, 19 bytes Input is a string, output is an integer: 0 for false or >0 for true. ã á3 ˬ¥U«ÓDˬxÃâ Ê Try it ã á3 ˬ¥U«ÓDˬxÃâ Ê :Implicit input of string U ã :Substrings á3 :Permutations of length 3 Ë :Map each D ¬ : Join ¥U : Equal to ...


0

Prolog, 43 bytes f(N,D):-N==0;O is N//10,D is N-O*10,f(O,D). Run the query with f(666,D) (or whatever other number you have). If it's a repdigit, it will return a value for D, otherwise, it will return false. Try it in SWISH (By the way, I think if you make changes directly without creating a new notebook, it may modify the original, so please don't mess ...


2

Jelly, 10 bytes 1Æd€<Ṫ$PƊ# Try it online! Returns the first \$n\$ highly composite numbers. My guess is that this uses features unavailable when Dennis first posted a Jelly answer, but this uses a sufficiently different algorithm that I thought it was ok to post as a separate answer How it works 1Æd€<Ṫ$PƊ# - Main link. Takes no arguments 1 Ɗ# - ...


0

Pip, 12 bytes UQ(a**\,4)%t takes range from 1 to 4, takes powers, mod 10, uniquify. Try it online!


1

Pip, 3 bytes $=a fold by equality. Try it online!


0

Jelly, 6 bytes ạƝḌ$ƬS Try it online! How it works ạƝḌ$ƬS - Main link. Takes an integer n on the left $Ƭ - Repeat the following until a repeated value, collected intermediate results: Ɲ - Convert an integer to digits, and on overlapping pairs: ạ - Take the absolute difference Ḍ - Convert from digits to an integer S - Take the ...


0

Jelly, 9 bytes D×7%⁵ƊƬḌS Try it online! How it works D×7%⁵ƊƬḌS - Main link. Takes an integer n on the left D - Convert to digits ƊƬ - Repeat the following until a value is repeated and collect all intermediate values: ×7 - Multiply each digit by 7 %⁵ - Take the ones digit Ḍ - Convert each list of digits back to a ...


1

APL (Dyalog Unicode), 21 17 bytes {⍵≤9:0⋄1+∇+/⍎¨⍕⍵} -4 bytes from Jo King. Try it online! {s←+/⍎¨⍕⍵⋄⍵≤9:0⋄1+∇s} Try it online! Explanation {s←+/⍎¨⍕⍵⋄⍵≤9:0⋄1+∇s} ⍵ → input ⍕⍵ convert ⍵ to string ⍎¨ execute each character(getting digits) +/ reduce to sum of digits s← assign to s ...


1

Pip, 13 bytes {aGTb?ab}MZab Maps zipped pairs of the two inputs and then returns the greater one. Inputs must be padded with spaces. Try it online!


0

C#, 42 bytes Using the power of fresh and new C# 9 we can achieve a stunning 42 bytes! int f(int n)=>n<2?1:n*f(n-1);return f(10); for C# 8 and the online example we need to add 38 bytes for a total of 70 bytes class P{static int Main(){int f(int n)=>n<2?1:n*f(n-1);return f(10);}} Try it online!


0

Dotty, 79 bytes import compiletime.ops.int._ type F[N<:Int]=N match{case 0=>1 case _=>N*F[N-1]} Try it in Scastie


0

Jelly, 10 bytes ḍƇ`ḊœċP€ċ⁸ Try it online! Assumes that "Built-in functions for factorization and/or partitioning are not allowed" doesn't prohibit the œċ builtin, which is combinations with replacements. If this isn't allowed, we can use a Cartesian product builtin, which is allowed, for 12 bytes: ḍƇ`ḊṗṢ€QP€ċ⁸ How it works ḍƇ`ḊœċP€ċ⁸ - Main link. ...


1

R, 35 bytes function(n,k)sum((a=(1:n)^k)*!n%%a) Try it online! 3 years later, but 6 bytes shorter than the previous R answer.


Top 50 recent answers are included