Millisecond in time.

W

Warren

Hi,

I have 1056030443784 (which is a time in millisecond) but I need this
time in the "normal" format like 2003-06-26 12:54.22

But I can't find any methods in ruby that can help me.
Any advice ?

Regards
W.B
 
Y

Yukihiro Matsumoto

Hi,

In message "Millisecond in time."

|I have 1056030443784 (which is a time in millisecond) but I need this
|time in the "normal" format like 2003-06-26 12:54.22

Time.at(1056030443784/1000.0).strftime("%Y-%m-%d %H:%M.%S")

matz.
 
H

Harry Ohlsen

Warren said:
Hi,

I have 1056030443784 (which is a time in millisecond) but I need this
time in the "normal" format like 2003-06-26 12:54.22

Time has an "at" method that takes a number of seconds and returns a time, so you could do something like ...

ms = 1056030443784

time = Time.at(ms / 1000)

puts time

It also takes a number of microseconds as a second argument, so you could also do

time = Time.at(ms / 1000, (ms % 1000) * 1000)

to get a more accurate time, if necessary. Apologies if my arithmetic is crap in the second example :).

H.
 
B

Brian Candler

wouldn't that be

time = Time.at(ms / 1000, ms % 1000)

'%' returns the fraction part *as an integer* iirc

but 1 millisecond is 1000 microseconds.
 
H

Harry Ohlsen

Anders said:
wouldn't that be

time = Time.at(ms / 1000, ms % 1000)

'%' returns the fraction part *as an integer* iirc

Yes, but the second argument to Time#at is in microseconds.

Since the original value was milliseconds, taking it mod 1000 gives us just the leftover milliseconds (ie, the ones that don't add up to a multiple of a second).

We then need to multiply that by 1000, since microseconds are 1000 times smaller than milliseconds.

Or is my logic still screwed up? As I said, my arithmetic is terrible :).
 
H

Harry Ohlsen

Given that we're talking about modulo arithmetic :) ...

I'm sitting here trying to write some Ruby code that generates the "magic table" from the remainders that pop up when executing Euclid's algorithm. I don't suppose anyone happens to remember how that works?

I'm writing the Ruby partly to remind myself of the number theory, but with the intention of producing something that my old lecturer might be able to use to allow him to easily come up with new tutorial, quiz and assignment questions, without having to slog through the calculations.

You can see an example of such a table in the solution to question 7, part (b) in the following PDF ...

http://www.maths.usyd.edu.au:8000/u/terry/3009/02nt01s.pdf

I remember it has to do with multiplying two diagonal elements then adding one on the left, but can't for the life of me work out which ones !!

Cheers,

Harry O.
 
T

ts

H> I'm sitting here trying to write some Ruby code that generates the
H> "magic table" from the remainders that pop up when executing Euclid's
H> algorithm. I don't suppose anyone happens to remember how that works?

Euh, this ???


svg% cat b.rb
#!/usr/bin/ruby
def lcr(a, b, v)
r = a % b
if r == 0
[]
else
q = a / b
x = [v[0] - q * v[2], v[1] - q * v[3]]
[x] + lcr(b, r, [v[2], v[3], *x])
end
end

def lc(a, b)
if b == 0
[[1, 0]]
else
lcr(a, b, [1, 0, 0, 1])
end + [[a, b]]
end

p lc(93512222, 6812345)
svg%

svg% b.rb
[[1, -13], [-1, 14], [3, -41], [-4, 55], [7, -96], [-11, 151], [227, -3116], [-919, 12615], [42501, -583406], [-43420, 596021], [1345101, -18464036], [-2733622, 37524093], [93512222, 6812345]]
svg%


Guy Decoux
 
H

Harry Ohlsen

ts said:
Euh, this ???


svg% cat b.rb
#!/usr/bin/ruby
def lcr(a, b, v)
r = a % b
if r == 0
[]
else
q = a / b
x = [v[0] - q * v[2], v[1] - q * v[3]]
[x] + lcr(b, r, [v[2], v[3], *x])
end
end

def lc(a, b)
if b == 0
[[1, 0]]
else
lcr(a, b, [1, 0, 0, 1])
end + [[a, b]]
end

p lc(93512222, 6812345)
svg%

svg% b.rb
[[1, -13], [-1, 14], [3, -41], [-4, 55], [7, -96], [-11, 151], [227, -3116], [-919, 12615], [42501, -583406], [-43420, 596021], [1345101, -18464036], [-2733622, 37524093], [93512222, 6812345]]
svg%

I'm not sure about all those negative numbers, but a heck of a lot of the values look right. So, I think your code is definitely a good starting point for me.

It'll save me going down into the garage tonight ... it's 9:50pm here in Sydney ... to try and find my notes, anyway :).

Thanks!
 
H

Harry Ohlsen

ts said:
H> I'm sitting here trying to write some Ruby code that generates the
H> "magic table" from the remainders that pop up when executing Euclid's
H> algorithm. I don't suppose anyone happens to remember how that works?

Euh, this ???


svg% cat b.rb
#!/usr/bin/ruby
def lcr(a, b, v)
r = a % b
if r == 0
[]
else
q = a / b
x = [v[0] - q * v[2], v[1] - q * v[3]]
[x] + lcr(b, r, [v[2], v[3], *x])
end
end

def lc(a, b)
if b == 0
[[1, 0]]
else
lcr(a, b, [1, 0, 0, 1])
end + [[a, b]]
end

p lc(93512222, 6812345)
svg%

svg% b.rb
[[1, -13], [-1, 14], [3, -41], [-4, 55], [7, -96], [-11, 151], [227, -3116], [-919, 12615], [42501, -583406], [-43420, 596021], [1345101, -18464036], [-2733622, 37524093], [93512222, 6812345]]
svg%


Guy Decoux

I was able to get your code to produce the table precisely as it was in the exercise solution by making a couple of tiny changes, although I'm going to have to think about how your recursive code relates to my simplistic implementation of the Euclidean algorithm :) ...


def lcr(a, b, v)
r = a % b
if r == 0
[]
else
q = a / b
x = [v[0] - q * v[2], v[1] - q * v[3]]
[x] + lcr(b, r, [v[2], v[3], *x])
end
end

def lc(a, b)
if b == 0
[[1, 0]]
else
lcr(a, b, [1, 0, 0, 1])
end + [[b, a]]
end

p lc(93512222, 6812345).map { |x| [x[1].abs, x[0].abs] }

D:\HarryO\Mathematics\NumberTheory>ruby guy.rb

[[13, 1], [14, 1], [41, 3], [55, 4], [96, 7], [151, 11], [3116, 227], [12615, 91
9], [583406, 42501], [596021, 43420], [18464036, 1345101], [37524093, 2733622],
[93512222, 6812345]]
 
H

Harry Ohlsen

ts said:

Thanks again, Guy. Based on that reference, I was able to get my naive (non-recursive) implementation working and generating the correct magic table ...


module Euclid
class MagicTable < Array
Row = Struct.new("Row", :d, :p, :q)

def initialize
self << Row.new(nil, 0, 1)
self << Row.new(nil, 1, 0)
end

alias :<< push

def <<(row)
push(Row.new(*row))
end

def to_s
s = ""

self.each do |row|
d = row.d

if d.nil?
s << " "
else
s << (sprintf "%4d ", d)
end
end

s << "\n"

self.each { |row| s << (sprintf "%4d ", row.p) }

s << "\n"

self.each { |row| s << (sprintf "%4d ", row.q) }

s << "\n"

return s
end
end

def Euclid.GCD(a, b)
magic = MagicTable.new

if a < b
a, b = b, a
end

while b > 0
d = a / b
r = a % b

p = d * magic[-1].p + magic[-2].p
q = d * magic[-1].q + magic[-2].q

magic << [d, p, q]

a, b = b, a % b
end

return a, magic
end

end

if $0 == __FILE__
gcd, magic = Euclid.GCD(787, 268)

puts "(787, 268) = #{gcd}\n\n"

puts "Magic Table ...\n\n#{magic}"
end
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top