Skip to main content
added 94 characters in body
Source Link
clismique
  • 7k
  • 27
  • 72

ClojureClojure, 6757 bytes

(defn i[n](if(= n 1)true(if(or(= n 0)(ratio? n))false(i(/ n -2)))))
(defn i[n](if(= n 1)true(if(=(int n)0)false(i(/ n -2)))))

Sigh...Try it online!

Here's the expanded version of theFull function, with annotations:

;; Define the function, `is-pow?` with
;; 1 argument, `n`
(defn is-pow? [n]
  ;; If n = 1, then return true -
  ;; n is a power of -2
  (if (= n 1) true
    ;; Clojure has fractions (called ratios here),
    ;; so 5 / -2 = -5/2. This captures the edgecase
    ;; of 0, and determines if a number is not a
    ;; power of -2.
    (if (or (= n 0) (ratio? n)) false
      ;; If none of this checks out, repeat the function,
      ;; But divide the number by -2 first
      (is-pow? (/ n -2))))
;; Define function `is-pow?` with 1 argument, `n`
(defn is-pow? [n]
  ;; If n = 1, that means it's a power of -2,
  ;; so we return true
  (if (= n 1) true
    ;; When we recursively call the function,
    ;; -1 > n > 1. `int` rounds up when the number
    ;; is negative (`(int -1/2)` = 0), and rounds down
    ;; when the number is positive. It also catches
    ;; the edgecase of 0.
    (if (= (int n) 0) false
      ;; If n made it to here, n < -1 or n > 1 - we have
      ;; to call the function recursively.
      (is-pow? (/ n -2)))))

Clojure, 67 bytes

(defn i[n](if(= n 1)true(if(or(= n 0)(ratio? n))false(i(/ n -2)))))

Sigh...

Here's the expanded version of the function:

;; Define the function, `is-pow?` with
;; 1 argument, `n`
(defn is-pow? [n]
  ;; If n = 1, then return true -
  ;; n is a power of -2
  (if (= n 1) true
    ;; Clojure has fractions (called ratios here),
    ;; so 5 / -2 = -5/2. This captures the edgecase
    ;; of 0, and determines if a number is not a
    ;; power of -2.
    (if (or (= n 0) (ratio? n)) false
      ;; If none of this checks out, repeat the function,
      ;; But divide the number by -2 first
      (is-pow? (/ n -2))))

Clojure, 57 bytes

(defn i[n](if(= n 1)true(if(=(int n)0)false(i(/ n -2)))))

Try it online!

Full function, with annotations:

;; Define function `is-pow?` with 1 argument, `n`
(defn is-pow? [n]
  ;; If n = 1, that means it's a power of -2,
  ;; so we return true
  (if (= n 1) true
    ;; When we recursively call the function,
    ;; -1 > n > 1. `int` rounds up when the number
    ;; is negative (`(int -1/2)` = 0), and rounds down
    ;; when the number is positive. It also catches
    ;; the edgecase of 0.
    (if (= (int n) 0) false
      ;; If n made it to here, n < -1 or n > 1 - we have
      ;; to call the function recursively.
      (is-pow? (/ n -2)))))
Source Link
clismique
  • 7k
  • 27
  • 72

Clojure, 67 bytes

(defn i[n](if(= n 1)true(if(or(= n 0)(ratio? n))false(i(/ n -2)))))

Sigh...

Here's the expanded version of the function:

;; Define the function, `is-pow?` with
;; 1 argument, `n`
(defn is-pow? [n]
  ;; If n = 1, then return true -
  ;; n is a power of -2
  (if (= n 1) true
    ;; Clojure has fractions (called ratios here),
    ;; so 5 / -2 = -5/2. This captures the edgecase
    ;; of 0, and determines if a number is not a
    ;; power of -2.
    (if (or (= n 0) (ratio? n)) false
      ;; If none of this checks out, repeat the function,
      ;; But divide the number by -2 first
      (is-pow? (/ n -2))))