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
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
Hi,
In message “Millisecond in time.” on 03/07/01, Warren place4oracle@yahoo.com writes:
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.
Warren wrote:
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.
Harry Ohlsen wrote:
Warren wrote:
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.22Time 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 dotime = Time.at(ms / 1000, (ms % 1000) * 1000)
wouldn’t that be
time = Time.at(ms / 1000, ms % 1000)
‘%’ returns the fraction part as an integer iirc
to get a more accurate time, if necessary. Apologies if my arithmetic
is crap in the second example :-).H.
–
dc -e
4ddod3dddn1-89danrn10-dan3+ann6dan2an13dn1+dn2-dn3+5ddan2/9+an13nap
but 1 millisecond is 1000 microseconds.
On Tue, Jul 01, 2003 at 07:28:48PM +0900, Anders Borch wrote:
It also takes a number of microseconds as a second argument, so you
could also dotime = Time.at(ms / 1000, (ms % 1000) * 1000)
wouldn’t that be
time = Time.at(ms / 1000, ms % 1000)
‘%’ returns the fraction part as an integer iirc
Anders Borch wrote:
It also takes a number of microseconds as a second argument, so you
could also dotime = Time.at(ms / 1000, (ms % 1000) * 1000)
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 :-).
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.
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?
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]]
+ 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
ts wrote:
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]]
+ lcr(b, r, [v[2], v[3], *x])
end
enddef lc(a, b)
if b == 0
[[1, 0]]
else
lcr(a, b, [1, 0, 0, 1])
end + [[a, b]]
endp 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!
ts wrote:
“H” == Harry Ohlsen harryo@qiqsolutions.com writes:
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?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]]
+ lcr(b, r, [v[2], v[3], *x])
end
enddef lc(a, b)
if b == 0
[[1, 0]]
else
lcr(a, b, [1, 0, 0, 1])
end + [[a, b]]
endp 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]]
+ 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]]
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 ...
Well, if I'm remember correctly : if (p, q) are number in the "magic
table", then
p = p[k-1] * (-1)**k-1
q = q[k-1] * (-1)**k
where p[k-1], q[k-1] are the value given by the Euclidean algorithm
This must explain the negative number becuase it give the result of the
Euclidena algorithm.
Guy Decoux
ts wrote:
Well, if I’m remember correctly : if (p, q) are number in the “magic
table”, thenp = p[k-1] * (-1)**k-1
q = q[k-1] * (-1)**kwhere p[k-1], q[k-1] are the value given by the Euclidean algorithm
This must explain the negative number becuase it give the result of the
Euclidena algorithm.
Thanks, Guy.
Obviously, your definition of the table is slightly different to my old lecturer’s, because his doesn’t generate any negative values. I’ll just have to sit down and work out where the approaches differ.
That’s not a problem to me, because I’m keen to understand it. That’s my reason for writing the code, after all :-).
Cheers,
Harry O.
Obviously, your definition of the table is slightly different to my old
lecturer's, because his doesn't generate any negative values. I'll just
have to sit down and work out where the approaches differ.
It came from here :
http://www.maths.usyd.edu.au:8000/u/bobh/UoS/MATH3009/wk2.pd
Guy Decoux
http://www.maths.usyd.edu.au:8000/u/bobh/UoS/MATH3009/wk2.pd
^^^
it's pdf (the `f' is missing)
http://www.maths.usyd.edu.au:8000/u/bobh/UoS/MATH3009/wk2.pdf
Guy Decoux
ts wrote:
It came from here :
http://www.maths.usyd.edu.au:8000/u/bobh/UoS/MATH3009/wk2.pd
How amazing! Bob Howlett (the author of that PDF) is one of my other old lecturers :-).
I’ll have to point out to he and Terry Gagen (the person I’m hoping to help with this code) that maybe they should get their stories straight :-).
Harry O.
ts wrote:
It came from here :
http://www.maths.usyd.edu.au:8000/u/bobh/UoS/MATH3009/wk2.pd
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
By the way, thanks for that link. It’s basically what I was looking for before I asked the original question!