Re: [QUIZ] Reverse Divisible Numbers (#161)
- From: Ken Bloom <kbloom@xxxxxxxxx>
- Date: Sun, 4 May 2008 14:20:02 -0500
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/
.
- Follow-Ups:
- Re: [QUIZ] Reverse Divisible Numbers (#161)
- From: Sandro Paganotti
- Re: [QUIZ] Reverse Divisible Numbers (#161)
- References:
- [QUIZ] Reverse Divisible Numbers (#161)
- From: Matthew Moss
- [QUIZ] Reverse Divisible Numbers (#161)
- Prev by Date: Re: Don't wait for Exception in threads
- Next by Date: Re: Different Ways To Loop
- Previous by thread: Re: Reverse Divisible Numbers (#161)
- Next by thread: Re: [QUIZ] Reverse Divisible Numbers (#161)
- Index(es):
Relevant Pages
|
Loading