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

Scientific Notation

An easy problem for this fine Tuesday. Your goal is to create a function that takes in an integer. Then you must print out that integer in scientific notation. For example:

10000 = 1 x 104

Feel free to assume the integer input will always be greater than 0.

Permalink: http://problemotd.com/problem/scientific-notation/

Comments:

  • Derek - 9 years, 11 months ago

    In Ruby:

     def sci(i)
       mant = ""
       e = 0
       while i > 10
         d = i%10
         mant = "#{d}#{mant}" unless mant.empty? and d == 0
         i /= 10
         e += 1
       end
       "#{i}.#{mant} x 10^#{e}"
     end
    

    reply permalink

  • Derek - 9 years, 11 months ago

    ah crap.

     def sci(i)
       mant = ""
       e = 0
       while i >= 10
         d = i%10
         mant = "#{d}#{mant}" unless mant.empty? and d == 0
         i /= 10
         e += 1
       end
       "#{i}.#{mant} x 10^#{e}"
     end
    

    reply permalink

  • Derek - 9 years, 11 months ago

    Third time's the charm. ruby def sci(i) mant = "" e = 0 while i >= 10 d = i%10 mant = "#{d}#{mant}" unless mant.empty? and d == 0 i /= 10 e += 1 end mant = "0" if mant.empty? "#{i}.#{mant} x 10^#{e}" end

    reply permalink

  • Derek - 9 years, 11 months ago

    Clearly I need more coffee.

     def sci(i)
       mant = ""
       e = 0
       while i >= 10
         d = i%10
         mant = "#{d}#{mant}" unless mant.empty? and d == 0
         i /= 10
         e += 1
       end
       mant = "0" if mant.empty?
       "#{i}.#{mant} x 10^#{e}"
     end
    

    reply permalink

  • Max Burstein - 9 years, 11 months ago

    Needs more coffee script haha, Nicely done!

    reply permalink

Content curated by @MaxBurstein