r/dailyprogrammer • u/jnazario 2 0 • Jul 05 '17
[2017-07-05] Challenge #322 [Intermediate] Largest Palindrome
Description
Write a program that, given an integer input n, prints the largest integer that is a palindrome and has two factors both of string length n.
Input Description
An integer
Output Description
The largest integer palindrome who has factors each with string length of the input.
Sample Input:
1
2
Sample Output:
9
9009
(9 has factors 3 and 3. 9009 has factors 99 and 91)
Challenge inputs/outputs
3 => 906609
4 => 99000099
5 => 9966006699
6 => ?
Credit
This challenge was suggested by /u/ruby-solve, many thanks! If you have a challenge idea, please share it in /r/dailyprogrammer_ideas and there's a good chance we'll use it.
72
Upvotes
2
u/MattieShoes Jul 05 '17 edited Jul 05 '17
Straightforward C++11 implementation (
g++ -std=c++11)Output:
I'm guessing there's probably a reasonable way to put a lower bound on the search space or perhaps order the things you test more efficiently (that is, 98x98 is tested before 99x10).
12
u/[deleted] Jul 05 '17 edited Jul 05 '17
Python Probably took a different approach from others, I check every palindrome from highest to lowest and see if it's divisible by two n-digit numbers. Fairly efficient, 6 is done almost instantly and 7 takes a bit longer. Doesn't work for 1, but I don't feel like that's needed :)
8
u/J-Goo Jul 05 '17 edited Jul 05 '17
Python 2.6:
ETA: this algorithm is good for an input of 6 (5.82s) and horrendous for an input of 7 (over eight minutes).
6
u/skeeto -9 8 Jul 05 '17 edited Jul 05 '17
C, brute force
O(n2).Takes 4.7 seconds for n = 7.
3
u/Aussiemon Jul 06 '17
Java, my first-ever submission:
Could definitely be more efficient. Things got messy when I remembered integers have such a low max value.
Full-code: Link
3
u/Sud0nim Jul 06 '17 edited Jul 06 '17
Nim
Not very optimal I'm sure, but at least it was a fun one to make. I removed the benchmarking function because it obscures the actual code:
Input:
Output:
2
u/Charredxil Jul 05 '17
My awful terrible incredibly inefficient solution in Python 3, that is only one line after imports and input
2
u/gabyjunior 1 2 Jul 05 '17
Ruby
Check palindromes for all products of two n digits factors in descending order.
Takes 12 seconds for n = 7
1
u/FunkyNoodles Jul 05 '17 edited Jul 05 '17
The best I could come up with at the moment Python 2.7
1
u/svgwrk Jul 05 '17
This takes forever, but I'm not real clear on why. I tried using profiling to discover which bits were taking forever, buuuuuuut that wasn't particularly revealing because the profiler refused to name any of the functions it was sampling. :)
I imagine that this does basically the same thing that /u/skeeto's solution is doing, but it takes almost three times as long to do it. Maybe part of the difference is hardware, but there must be some kind of difference in semantics. Feel free to drop me a hint!
I did notice that the C solution has a smaller search space because /u/skeeto starts his second range from
arather than fromend, but I tried that and it made exactly zero difference in the runtime.It could be that the issue has something to do with copying the range values themselves? I dunno. I wouldn't think so because it seems like the inner for loop in C would have to do exactly the same thing.
1
u/Vyse007 Jul 05 '17
My terribly inefficient solution in Racket. I filter out all the factors of 10 from the list of pairs since I know they can't produce palindromes. Still takes a loooong time for n = 6 though.
1
u/DevOrc Jul 05 '17 edited Jul 05 '17
This is my first post here so here it goes. I wrote my implementation in rust.
Edit: 42 seconds for n = 7
1
u/curtmack Jul 05 '17
Common Lisp
About as efficient an implementation as I could knock out quickly - 6 takes about 6 seconds to calculate. Most of this time is lost to linear searches for duplicates - I'll try to improve on this later on after work.
1
u/macgillebride Jul 05 '17
An Haskell solution. It executes nicely up to n=8, but for n=9 it takes too long (it was taking longer than 5 min so I killed it). I wonder if it's possible to code something as efficient in a more concise way in Haskell.
1
u/austin_flowers Jul 05 '17
Python 3
Takes 1.03s for n=6 and 6.08s for n=7. However, it takes 116s for n=8. Obviously it would be more efficient for the larger numbers to make a big ol' list of palindromic numbers and then check against those. This approach seems fine when n<8 though.
1
u/[deleted] Jul 05 '17 edited Jul 05 '17
Python3
The code is optimized for even numbers (someone would call it cheating ;)) and little bit for odd numbers.
Code:
Output:
1
u/Godspiral 3 3 Jul 05 '17
in J, reasonably quick. Trial division by numbers greater than square root up to max for each possible palindrome.
999000
I thought a faster approach would take the factoring of the palindrome and from its combinations see if their products form 2 numbers the right length. Buts its about twice as slow 2.9 sec vs 1.4.
1
u/MisterMikeM Jul 06 '17
Golang
1
u/BronyaurStomp Jul 06 '17
C#
Pretty basic. 6 is 40ms and 7 is 30s, although that number may be inaccurate.
1
u/flatlandr Jul 06 '17
PHP
~55s for n=7
1
u/Riverdan4 Jul 06 '17
Really awful slow bruteforce method. took 846s ~= 14minutes to get 999000000999 for n=6
Python 3:
1
u/Scroph 0 0 Jul 06 '17 edited Jul 06 '17
Bruteforce in D but instead of looping over factors, it loops over palindromes.
For a length of 4, it takes the upper half of 1000 ^ 2 and that of 9999 ^ 2, loops over them then combines each with a mirrored version before checking to see if it has factors of length 4. The code is ugly and slow, it takes 1m54 for an input of 6 and 6 seconds for an input of 5.
This is without a doubt the worst thing I wrote this year, and that's taking into consideration the fact that I'm a right handed guy who tried to write down something on a piece of paper with his left hand a few months ago.
Edit : well what do you know ! The program just finished executing for an input of 7 :
1
u/slartibartfass_ Jul 06 '17
Rust:
1
u/King-Tuts Jul 06 '17 edited Jul 06 '17
C#
It's a little verbose, but I'm new to C#. It became a lot faster after I reduced the search space for valid factors. That and changing the factoring algorithm to return a boolean if it found a valid pair of factors, that way I could stop as soon as I found a valid pair.
Code:
Output:
1
u/JakDrako Jul 07 '17 edited Jul 07 '17
VB.Net
Very fun puzzle. Simple to solve (slowly), but very hard to get fast. I tried a lot of different optimizations, with the final realization that using math is much faster than arrays, string, etc.
One of the optimization that gave me trouble was ordering the factors in descending order. My idea was that when you have a multiplication table, the largest factors are at the bottom right and as you "zig zag" (sort of) your way toward the upper left, you hit all numbers in descending order. That way, the 1st result would also be the largest. Finally gave up and stole /u/Charredxil's method of ordering factors in descending order. I just increment/decrement by steps of 2 to skip even numbers.
Ze code:
Results:
1
u/hadron1337 Jul 08 '17 edited Jul 08 '17
Rust
I build up the possible Inputs in a pyramide like scheme and traverse them top to bottom (excluding mirrored multiplications) Needs for n=7 around 0.5 seconds, 2.5 for n=8 and about 78 Seconds for n=9 Example:
99*99
98*99 99*98
97*99 98*98 99*97
96*99 97*98 98*97 99*96
1
u/joesacher Jul 08 '17 edited Jul 11 '17
Go
First real program ever in Go and first submission here.
Output
Overflows > 9.
1
u/A-Grey-World Jul 10 '17 edited Jul 10 '17
My solution in Javascript. My first thought was to search a list of known (or build a list of palindromes), but I decided just to search down the largest factors.
I found the way 'count down' in factors visually using excel:
http://i.imgur.com/0yxw8hd.png
Performance isn't great above 6, which it can do in about 150-250ms in Chrome (999999x999001 = 999000000999), 7 takes 2.8-3.2 seconds (9998017x9997647 = 99956644665999). I'd probably have to run 8 in node to get a sensible timing. Chrome just doesn't seem to come back with the answer.
1
u/popillol Jul 10 '17
Go / Golang Playground Link. Brute-force, terribly slow for input > 3.
Code:
1
u/TheThinker_SK Jul 10 '17 edited Jul 11 '17
C++ 11 My solution works but it has pretty bad complexity. It might be a little slow but it gets the job done.
Edit: Optimized the palindrome checking. The fast solutions posted in this subreddit are really good. I'm really amazed at what people here have came up with. Now I wanna keep tinkering with ym code and optimizing every bit possible.
Edit2: This is the most optimized version I can come up with. I does the low inputs (1-8) pretty fast.
1
u/stubby441 Jul 10 '17
Java Just found this subreddit last night and thought I'd give one of these a try! I'm a beginner (my only experience comes from AP Comp Sci at my high school) so I'd love any feedback you have. It computed the length of 6 almost instantly and 7 in 16.1 seconds
The brief pseudocode is because I was in a rush to finish because of time constraints. I think that the best place to probably speed the program up is within the nested for loop, I feel like there's probably a more efficient way than nesting the factors like I did but the program works so oh well :)
1
u/ChemiCalChems Jul 11 '17
C++ So after optimizing I came up to 9 relatively quickly, and seeing that 9 hits ull's limit, I went ham and started working with big nums. Currently computing, will keep on updating once I get more results.
1
u/TheMsDosNerd Jul 11 '17 edited Jul 11 '17
Python, Ugly single line solution:
EDIT: made it a bit shorter
1
u/[deleted] Jul 13 '17
Java -- Slow, brute force solution, but it works and I'm new at this!
1
u/mr_smartypants537 Jul 16 '17
C++ Solution, base agnositic
1
u/paypaytr Aug 05 '17 edited Aug 05 '17
C++.It takes its sweet time to calculate though its very simple.
1
u/[deleted] Aug 27 '17 edited Aug 28 '17
Ruby
This solution is port of /u/Peet79's great python solution. Solves for n = 6 in 0.058971 seconds, and n = 7 in 0.919189 seconds.
benchmarks:
0
u/[deleted] Jul 05 '17
[deleted] — view deleted comment
0
u/[deleted] Jul 05 '17
[deleted] — view deleted comment
0
u/[deleted] Jul 06 '17
[deleted] — view deleted comment