Problem of the Day
A new programming or logic puzzle every Mon-Fri

isOdd Hard

Today's challenge is a new take on a classic. Without using the modulus operator or converting the number to binary to check the least significant bit, can you write a function to determine if an integer is odd or not?

Permalink: http://problemotd.com/problem/isodd-hard/

Comments:

  • Anonymous - 10 years, 1 month ago

    def isOdd(n):
        return n != int(n/2)*2
    

    reply permalink

  • Akshay Bist - 10 years, 1 month ago

    Prime or odd? Which is it?

    reply permalink

  • Max Burstein - 10 years, 1 month ago

    My bad on that. Meant to say odd

    reply permalink

  • Driphter - 10 years, 1 month ago

    Clojure!

    (defn odd? [x]
      (let [half (float (/ x 2))]
        (not= half (Math/floor half))))
    
    (defn prime? [x]
      (let [y (float (/ x 2))
            y* (Math/floor y)]
        (if (= y y*)
          false
          (loop [s (vec (range 3 y*))]
            (if (empty? s)
              true
              (let [y (float (/ x (first s)))
                    y* (Math/floor y)]
                (if (= y y*)
                  false
                  (recur (remove #(= % y*) (next s))))))))))
    

    reply permalink

  • phansen73 - 10 years, 1 month ago

    C++

    int isOdd (int x) {
      if (x == x/2*2) {
        return 0;
      } 
      return 1;
    }
    
    

    reply permalink

  • asheehan - 10 years, 1 month ago

    felt like playing with floor... in Ruby

    def is_odd(x)
        y = ((x - 1) / 2).floor
        x = (x / 2).floor
        return (x == y)
    end
    
    def is_whole(x)
        return (x == x.floor and x == x.ceil)
    end
    
    def is_prime(x)
        if is_odd(x)
            n = (x / 2).floor
            if !is_odd(n)
                n -= 1
            end
            while n > 1 do
                if is_whole(x.to_f / n)
                    return false
                end
                n -= 2
            end
            return true
        end
        return false
    end
    
    p is_prime(37)
    

    reply permalink

  • Jojo Masala - 9 years, 10 months ago

    Java:

    private boolean nIsOdd(double n) { boolean isOdd = false; n /= 2.0; double roundDown = (int) n; double roundUp = roundDown + 1; if((n > roundDown) && (n < roundUp)) { isOdd = true; } return isOdd;

    reply permalink

Content curated by @MaxBurstein