Ad
Algorithms

Using base-10 logarithm

Code
Diff
  • #include <math.h>
    typedef signed long long i64;
    
    int num_of_digits(i64 n){
      i64 k = n < 0 ? -n : n;
      return log10(k) + 1;
    }
    
    • fn digits(mut n: u64) -> usize {
    • let mut l = 1;
    • while n >= 10 {
    • n /= 10;
    • l += 1;
    • }
    • l
    • }
    • #include <math.h>
    • typedef signed long long i64;
    • int num_of_digits(i64 n){
    • i64 k = n < 0 ? -n : n;
    • return log10(k) + 1;
    • }
Code
Diff
  • def oddEven(n):
        try:
            if n % 2:
                oddEven(n)
            else:
                return 0
        except RecursionError:
            return 1
    
    • def oddEven(n):
    • return not str(n)[-1] in "02468"
    • try:
    • if n % 2:
    • oddEven(n)
    • else:
    • return 0
    • except RecursionError:
    • return 1

Arithmetic Shift Right by n is equivalent to dividing by 2^n.

Code
Diff
  • typedef unsigned long long ull;
    ull div2(ull a) { return a >> 1; }
    • #define ull unsigned long long
    • ull div2(ull a) { return a * 0.5; }
    • typedef unsigned long long ull;
    • ull div2(ull a) { return a >> 1; }