1
$\begingroup$

I am struggling to create a smart algorithm. Currently I have produced one which is super-ugly and not efficient because I am casting numerics to strings back and forth, not really useful to share it here.

The point of algorithm is to get a number 10^x which is smaller than a given one.

Examples:

  • 600 => 100
  • 34.54 => 10
  • 7.2 => 1
  • 0.123 => 0.1

I am sure I am missing something. Is there any smart way of doing that?

$\endgroup$
1
$\begingroup$

$Num \geq 10^{x} $

$\implies x \leq log_{10}(Num)$

Then $x = \lfloor log_{10}(Num) \rfloor $.

$10^{x}$ would be your number.

$\endgroup$
  • 1
    $\begingroup$ Thank you, this obviously works like a charm! Now I am not ashamed of the code produced (instead I am ashamed of forgetting logarithms), thanks! :) $\endgroup$ – Pijotrek Jan 3 '18 at 14:18
  • $\begingroup$ We can learn anytime, thanks :) @Pijotrek $\endgroup$ – Mr. Sigma. Jan 3 '18 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.