# Tag Info

93

x86_64 machine code, 4 bytes The BSF (bit scan forward) instruction does exactly this! 0x0f 0xbc 0xc7 0xc3 In gcc-style assembly, this is: .globl f f: bsfl %edi, %eax ret The input is given in the EDI register and returned in the EAX register as per standard 64-bit c calling conventions. Because of two's complement binary ...

56

C, 167503724710 Here's my solution to the problem. I admit it is unlikely to win a strict code golf competition, but it doesn't use any tricks to indirectly call built-in division functionality, it is written in portable C (as the original Stack Overflow question asked for), it works perfectly for negative numbers, and the code is exceptionally clear and ...

41

Jelly, 1 byte ḍ This took me hours to golf. Try it online!

39

Ruby 28 b=->n{n.to_s(3).chop.to_i 3} To divide by 3 we just need to remove the trailing zero in base 3 number: 120 -> 11110 -> 1111 -> 40 Works with negatives: ice distantstar:~/virt/golf [349:1]% ruby ./div3.rb 666 222 ice distantstar:~/virt/golf % ruby ./div3.rb -15 -5 Ruby, 6045 Alternatively, w/o using base conversion: ...

29

Brain-Flak, 72 70 64 62 58 46 bytes {({}[()]{(<()>)}{}<({}[()]<({}())>)>)}{}{{}}{} Takes dividend and divisor (in that order) as input and prints the divisor (truthy) or nothing. Since each stack has an implicit, infinite amount of zeroes, empty output should be considered falsy. While not stack-clean, this solution uses only a single ...

27

Mathematica, 13 chars Mean@{#,0,0}&

25

Python, 25 bytes lambda n:len(bin(n&-n))-3 n & -n zeroes anything except the least significant bit, e.g. this: 100010101010100000101010000 v 000000000000000000000010000 We are interested in the number of trailing zeroes, so we convert it to a binary string using bin, which for the above number will be "0b10000". Since we don't care ...

23

Jelly, 4 bytes Æfċ2 In the latest version of Jelly, ÆEḢ (3 bytes) works. Æf Calculate the prime factorization. On negative input, -1 appended to the end. ċ2 Count the 2s. Try it here.

23

Golfscript, 27 22 bytes {.9>{.10/\10%2*-f}*}:f You can use it this way: 1000f Explanation {.9>{.10/\10%2*-f}*}:f { }:f # Define block 'f' (similar to a function) . # Duplicate the first value of the stack 9>{ }* # If the value on top of the stack is greater than 9 then the block is ...

23

BitCycle, 146 79 64 bytes Just realized a whole section of my original code was unneccessary. Huge reduction! v <>! A\B^^= ? D^>^< >\v^~ D@ >/ C/ > C ^ A/B v ^ < ? D^ The program takes input in unary from the command line, with the divisor first. It outputs the quotient and remainder in unary, separated by a 0. For example, ...

21

Pyth, 6 bytes /P.aQ2 Try it here. P.aQ In the prime factorization of the absolute value of the input / 2 count the number of 2s.

20

x86_32 machine code, 8 bytes 08048550 <div7>: 8048550: 99 cdq 8048551: f7 f9 idiv %ecx 8048553: 85 d2 test %edx,%edx 8048555: 0f 94 c0 sete %al This is my first code golf answer, so hopefully I'm following all the rules. This first calls cdq to clear out ...

19

Pyth, 5 l{yPQ Uses the subsets operation on the prime factors of the input, then keeps only the unique lists of factors and returns this count. Test Suite Explanation Using 25 as an example, so that the subset list isn't very long l{yPQ ## implicit: Q = eval(input()) so Q == 25 PQ ## Prime factors of Q, giving [5, 5] y ## All ...

19

05AB1E, 2 bytes ÑO Try it online! How? Ñ Divisors O Sum

18

JavaScript, 56 alert(Array(-~prompt()).join().replace(/,,,/g,1).length) Makes a string of length n of repeating ,s and replaces ,,, with 1. Then, it measures the string's resulting length. (Hopefully unary - is allowed!)

17

Writing rules is hard, these rules in particular contain incentive to avoid additions at all costs. Is there a prize for the most ridiculous answer? JavaScript - 0 additions Now with fallback method that does a hulking solution for larger a's and b's, and a slightly more compact structure in order not to bust the character limit. (Pfff, 30000 characters. ...

17

Brain-flak 102, 98, 96 bytes (({}<>))<>{({}[()])<>(({}[()])){{}(<({}[({})])>)}{}({}({}))<>}{}<>([{}]{}){<>(([()])())}({}{}()) Eww. Gross. I might post an explanation, but I barely understand it myself. This language hurts my brain. Try it online! Thanks to github user @Wheatwizard for coming up with a modulus ...

17

Hexagony, 15, 13, 12 10 bytes Everybody's favorite hexagon-based language! :D TL;DR works using magic, unformatted solutions in decreasing byte count: ?{?..>1'%<.@!'/ ?{?!1\.'%<@.> ?{?\!1@'%\!( ?{?!1\@'%< Saved 2 bytes thanks to @MartinEnder's layout wizardry. @FryAmTheEggman saved 1 byte by using the corners more creatively Both @...

17

Java 8, 44 42 41 39 bytes Crossed out 44 is still regular 44 ;( n->{int r=0;for(;~-n%--r<1;);return~r;} -2 bytes thanks to @LeakyNun. -1 byte thanks to @TheLethalCoder. -2 bytes thanks to @Nevay. Explanation: Try it here. n->{ // Method with integer as parameter and return-type int r=0; // Result-integer (starting ...

16

Python, 41 38 print"-"[x:]+len(xrange(2,abs(x),3)) xrange seems to be able to handle large numbers (I think the limit is the same as for a long in C) almost instantly. >>> x = -72 -24 >>> x = 9223372036854775806 3074457345618258602

16

JavaScript (ES6), 31 29 27 bytes Takes input as a string. Returns zero for truthy and non-zero for falsy. n=>n%eval([...n+n].join+) Commented n => n % eval([...n + n].join+) n => // take input string n -> e.g. "80" n + n // double the input -> "8080" [...

15

J, 45 44 10 chars ".,&'r3'": Works with negatives: ".,&'r3'": 15 5 ".,&'r3'": _9 _3 ".,&'r3'": 3e99 1e99 ": - format as text ,&'r3' - append r3 to the end ". - execute the string, e.g. 15r3

15

JavaScript (ES6), 18 bytes n=>Math.log2(n&-n) 4 bytes shorter than 31-Math.clz32. Hah.

15

brainfuck, 43 41 bytes ,<,[>->+<[>]>>>>+<<<[<+>-]<<[<]>-]>>.>>>. This uses a modified version of my destructive modulus algorithm on Esolangs. The program reads two bytes – d and n, in that order – from STDIN and prints two bytes – n%d and n/d, in that order – to STDOUT. It requires a ...

14

C++ C, 43 57 56 46 43 bytes On Martin Büttner's suggestions : i,c;f(n){for(i=c=n;i;n%i--&&--c);return c;}

14

Haskell, 45 37 34 bytes (+)>>=until((reverse>>=(==)).show)

13

C Not exactly a long division - this answer uses the method used in the real old days. #include <stdio.h> #include <stdlib.h> int main() { int a,b; scanf("%d %d", &a, &b); int *p=calloc(b, sizeof(int)); int *q=p; while(a--) { (*p)++; if(p-q<b-1) p++; else p-=b-1; } p=q; int r=0, ...

13

JavaScript ES6, 22 19 bytes f=x=>x%2?0:f(x/2)+1 Looks like recursion is the shortest route.

13

Haskell, 35 bytes until(<71)(\n->div n 10-2*mod n 10) Usage example: until(<71)(\n->div n 10-2*mod n 10) 36893488147419103232 -> 32. Nothing much to explain, it's a direct implementation of the algorithm.

13

Pyth, 7 bytes *f_I*Q Try it online: Demonstration Explanation *f_I*QT)Q implicit endings, Q=input number f ) find the first number T >= 1, which satisfies: *QT product of Q and T ` as string _I is invariant under inversion (=palindrom) * Q multiply this number with Q and print

Only top voted, non community-wiki answers of a minimum length are eligible