algunas cosas para observar:
no se si soy yo, pero usar progn y setq en lisp no me convence, siempre pense al principio en hacer todo recursivo y con let, pero parece que se complica a veces (no conozco una forma de evitar los lets nesteados, debe haber una forma pero no la conozco).
en cuanto a erlang no me gusta la diferencia de and andalso y or y orelse, pero parece que esta ahi por razones historicas, el pattern matching es adictivo, no me gusta que no pueda poner ; en el ultimo guard de un case.
hasta ahora vengo resolviendo el problema primero en python, despues en erlang y despues en lisp, por ahi eso condiciona la forma en la que lo resuelvo, pero siempre trato de usar la solucion que mas se adapta al lenguaje.
aca va el codigo
python
def is_palindromic(number):
'''return True if the number is a palindrome'''
number = str(number)
limit_start = len(number) / 2
if len(number) % 2 == 1:
limit_end = limit_start + 1
else:
limit_end = limit_start
return number[limit_start:] == number[:limit_end][::-1]
def get_largest_palindrome():
'''get the largest 3 digit palindrome'''
temp = 0
for i in range(999, 0, -1):
for j in range(i, 0, -1):
value = i * j
if is_palindromic(value):
if temp < value:
temp = value
return temp
print get_largest_palindrome()
lisp
(defun is-palindrome (num)
(progn
(setf str (format nil "~a" num))
(setf size (length str))
(setf first-half (truncate (/ size 2)))
(if (evenp size)
(setf second-half first-half)
; else
(setf second-half (+ first-half 1)))
(equal (subseq str 0 first-half) (reverse (subseq str second-half size)))))
(defun get-largest-palindrome ()
(progn
(setf temp 0)
(loop for i from 999 downto 0 do
(loop for j from i downto 0 do
(progn
(setq value (* i j))
(if (and (is-palindrome value) (> value temp))
(setq temp value)))))
temp))
(print (get-largest-palindrome))
erlang
-module(ej_004).
-compile(export_all).
%-export([get_largest_palindrome/0]).
is_palindrome(Number) ->
String = integer_to_list(Number),
Size = string:len(String),
FirstHalf = Size div 2,
case Size rem 2 == 1 of
false -> SecondHalf = FirstHalf + 1;
true -> SecondHalf = FirstHalf + 2
end,
FirstPart = string:substr(String, 1, FirstHalf),
SecondPart = lists:reverse(string:substr(String, SecondHalf, Size)),
FirstPart == SecondPart.
get_largest_palindrome(0, 0, Accum) -> Accum;
get_largest_palindrome(X, 0, Accum) -> get_largest_palindrome(X - 1, X - 1, Accum);
get_largest_palindrome(X, Y, Accum) ->
Value = X * Y,
case is_palindrome(Value) andalso Value > Accum of
true -> NewValue = Value;
false -> NewValue = Accum
end,
get_largest_palindrome(X, Y - 1, NewValue).
get_largest_palindrome(X, Accum) -> get_largest_palindrome(X, X, Accum).
get_largest_palindrome() -> get_largest_palindrome(999, 0).






!


In "The Zen of Graphics Programming", Michael Abrash (a co-author of Quake and inventor of Mode X) wrote:
-------------
Our world is changing, and I'm concerned. By way of explanation, three anecdotes.
Anecdote the first: In one of his books, Frank Herbert, author of Dune, told me how he had once been approached by a friend who claimed he (the friend) had a killer idea for a SF story, and offered to tell it to Herbert. In return, Herbert had to agree that if he used the idea in a story, he'd split the money from the story with this fellow. Herbert's response was that ideas were a dime a dozen; he had more story ideas than he could ever write in a lifetime. The hard part was the writing, not the ideas.
Anecdote the second: I've been programming micros for 15 years, and been writing about them for more than a decade and, until about a year ago, I had never-not once!- had anyone offer to sell me a technical idea. In the last year, it's happened multiple times, generally via unsolicited email along the lines of Herbert's tale.
This trend toward selling ideas is one symptom of an attitude that I've noticed more and more among programmers over the past few years-an attitude of which software patents are the most obvious manifestation-a desire to think something up without breaking a sweat, then let someone else?s hard work make you money. Its an attitude that says, "I'm so smart that my ideas alone set me apart." Sorry, it doesn't work that way in the real world. Ideas are a dime a dozen in programming, too; I have a lifetime's worth of article and software ideas written neatly in a notebook, and I know several truly original thinkers who have far more yet. Folks, it's not the ideas; it's design, implementation, and especially hard work that make the difference.
Virtually every idea I've encountered in 3-D graphics was invented decades ago. You think you have a clever graphics idea? Sutherland, Sproull, Schumacker, Catmull, Smith, Blinn, Glassner, Kajiya, Heckbert, or Teller probably thought of your idea years ago. (I'm serious-spend a few weeks reading through the literature on 3-D graphics, and you'll be amazed at what's already been invented and published.) If they thought it was important enough, they wrote a paper about it, or tried to commercialize it, but what they didn't do was try to charge people for the idea itself.
A closely related point is the astonishing lack of gratitude some programmers show for the hard work and sense of community that went into building the knowledge base with which they work. How about this? Anyone who thinks they have a unique idea that they want to "own" and milk for money can do so-but first they have to track down and appropriately compensate all the people who made possible the compilers, algorithms, programming courses, books, hardware, and so forth that put them in a position to have their brainstorm.
Put that way, it sounds like a silly idea, but the idea behind software patents is precisely that eventually everyone will own parts of our communal knowledge base, and that programming will become in large part a process of properly identifylng and compensating each and every owner of the techniques you use. All I can say is that if we do go down that path, I guarantee that it will be a poorer profession for all of us - except the patent attorneys, I guess.
Anecdote the third: A while back, I had the good fortune to have lunch down by Seattle's waterfront with Neal Stephenson, the author of Snow Crash and The Diamond Age (one of the best SF books I've come across in a long time). As he talked about the nature of networked technology and what he hoped to see emerge, he mentioned that a couple of blocks down the street was the pawn shop where Jimi Hendrix bought his first guitar. His point was that if a cheap guitar hadn't been available, Hendrix's unique talent would never have emerged. Similarly, he views the networking of society as a way to get affordable creative tools to many people, so as much talent as possible can be unearthed and developed.
Extend that to programming. The way it should work is that a steady flow of informa-tion circulates, so that everyone can do the best work theyre capable of. The idea is that I don't gain by intellectually impoverishing you, and vice-versa; as we both compete and (intentionally or otherwise) share ideas, both our products become better, so the market grows larger and everyone benefits.
That's the way things have worked with programming for a long time. So far as I can see it has worked remarkably well, and the recent signs of change make me concerned about the future of our profession.