ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> ruby-talk
ruby-talk
Re: [QUIZ] Reverse Divisible Numbers (#161)
by Sandro Paganotti other posts by this author
May 4 2008 2:22PM messages near this date
Re: [QUIZ] Reverse Divisible Numbers (#161) | Re: [QUIZ] Reverse Divisible Numbers (#161)
Here's mine:

MAXNUM = (( ARGV[0].to_i == 0 ) ? 1000000 : ARGV[0].to_i)

numbers = (0...MAXNUM).to_a
divs = Array.new

t = Time.now

MAXNUM.downto(0) do |n|
    nts = n.to_s
    next if numbers[n].nil? or
            ((nts[-1]-48) == 0) or
            (nts[0]-48) < (nts[-1]-48)*2 or
            nts == nts.reverse

    if n % (v = nts.reverse.to_i) == 0
      divs << [n,v]
      numbers.delete(v)
    end
end

puts "Benchmark = #{Time.now - t} secondi"
puts divs.inspect


On Sun, May 4, 2008 at 7:40 PM, Ken Bloom <kbloom@[...].com>  wrote:
>  On Fri, 02 May 2008 10:28:00 -0500, Matthew Moss wrote:
> 
> 
> 
>  > This is a fairly simple quiz this week, as I'm in the middle of putting
>   > together a new website to replace the existing RQ2 website, which was
>   > supposed to be temporary. Hopefully the new one should be in place next
>   > week.
>   >
>   > -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
>   >
>   > The three rules of Ruby Quiz 2:
>   >
>   > 1.  Please do not post any solutions or spoiler discussion for this quiz
>   > until 48 hours have passed from the time on this message.
>   >
>   > 2.  Support Ruby Quiz 2 by submitting ideas as often as you can! (A
>   > permanent, new website is in the works for Ruby Quiz 2. Until then,
>   > please visit the temporary website at
>   >
>   >      <http://matthew.moss.googlepages.com/home>.
>   >
>   > 3.  Enjoy!
>   >
>   > Suggestion:  A [QUIZ] in the subject of emails about the problem helps
>   > everyone on Ruby Talk follow the discussion.  Please reply to the
>   > original quiz message, if you can.
>   >
>   > -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
>   >
>   > Quiz #161
>   > Reverse Divisible Numbers
>   >
>   > This week's quiz is borrowed from Jon Galloway (http://tinyurl.com/
>   > 5jvf37).
>   > Don't read the comments or solution there without trying this first!
>   >
>   > Your task is to write a bit of Ruby code that will find and report all
>   > integers that are divisible by their reverse. For example, 9801 is
>   > divisible by 1089.
>   >
>   > Your script should accept a single, optional argument to specify the
>   > upper
>   > limit of your search. If not provided, the default should be one
>   > million.
>   >
>   > There are two extra rules for finding these "reverse divisible" numbers:
>   >
>   > 1. Don't count palindromes; they are obviously reverse-divisible. 2.
>   > Don't count numbers ending with zero. Reversing such numbers forces you
>   >    to drop leading zeros on the divisor, and that's not as much fun.
> 
>   Everyone's posted benchmarks, but nobody's posted code yet. It's been
>   more than 48 hours, so I guess I'll start.
> 
>   MAX=1_000_000
> 
>   #we want pairs of reverse,num where num.to_s==reverse.to_s.reverse and
>   #num*multiplier == reverse for some integer multiplier where
>   #multiplier >= 2
>   #
>   #if multiplier is a fraction, then we would swap num and reverse to get an
>   #integer, so we'll do it in one direction only
>   #
>   #if multiplier is 1, then num is a palindrome
>   #
>   #if the most significant digit of num > 4 then all num*multiplier
>   #have more digits than reverse has, so we need not check those
> 
>   DIGITS=(Math.log10(MAX)).to_i
>   ranges=(0..DIGITS).collect{ |digits| 10**digits ...  5*10**digits}
>   ranges[-1]=ranges[-1].begin..MAX if MAX < ranges[-1].end
> 
>   ranges.each do |range|
>    range.each do |num|
>      ns=num.to_s
>      reverse=ns.reverse.to_i
>      next if num==reverse
>      next if ns[-1,1]=='0'
>      next unless (reverse/num)*num==reverse
>      puts "#{num} * #{reverse/num} = #{reverse}"
>    end
>   end
> 
> 
> 
> 
>   --
>   Ken (Chanoch) Bloom. PhD candidate. Linguistic Cognition Laboratory.
>   Department of Computer Science. Illinois Institute of Technology.
>   http://www.iit.edu/~kbloom1/
> 
> 



-- 
Go outside! The graphics are amazing!
Thread:
Matthew Moss
Harry Kakueki
Matthew Moss
ThoML
Marcelo
Matthew Moss
Harry Kakueki
Ken Bloom
Ken Bloom
Sandro Paganotti
Ken Bloom
Matthew Moss
ThoML
ThoML
ThoML
Marcelo
ThoML
ThoML
Harry Kakueki
Marcelo
Marcelo
Shane Emmons
ThoML
Ken Bloom
Matthew Moss
Martin DeMello
Shane Emmons
Matthew Moss
ThoML
Marcelo
Rob Biedenharn
Robert Klemme
Robert Klemme
Matthew Moss
Shane Emmons
Matthew Moss

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved