tag:blogger.com,1999:blog-32756711691737372452017-07-29T01:06:48.622-07:00Project Euler with MatlabAndrewhttp://www.blogger.com/profile/16514776987162810586noreply@blogger.comBlogger7125tag:blogger.com,1999:blog-3275671169173737245.post-48528398479001199292011-07-26T10:28:00.000-07:002011-07-26T10:28:19.200-07:00Problem 96 Complete (more or less)So...<br /><br />After more hours than I'm proud of, I was able to create a sudoku solver that works rather well. Beside the obvious eliminations to possible solutions to just one box (veritcal, horizontal and quadrant eliminations) I tried to add some more logical solutions. When I solve Sudoku puzzles (easy ones anyways) I use a method of checking for which number each box can be. In words, if I get a puzzle like the following:<br /><div class="separator" style="clear: both; text-align: center;"><a href="http://0.tqn.com/d/puzzles/1/0/v/c/sudokue73.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="319" src="http://0.tqn.com/d/puzzles/1/0/v/c/sudokue73.gif" width="320" /></a></div><div class="separator" style="clear: both; text-align: left;">I would start with the number 2. You can see that the first row has a 2 in it, and the second row has a 2 in it. This implies that there needs to be a 2 in the third row as well, which HAS to be found in the top right quadrant of numbers. In this top right 3x3 quadrant, there are only two spaces that can be that 2 and one of them has a 2 in its column already. That means that only one box in this top right quadrant can be a 2, and I fill it in. Apparently, after some googling, I found out this is called hidden singles. Upon adding this selection method and re-iterating (as well as adding a solution counter), I was able to solve 40 of the 50 problems.</div><div class="separator" style="clear: both; text-align: left;"><br /></div><div class="separator" style="clear: both; text-align: left;">At this point I conceded the logical method and began implementing a bruting method. I just looked for the first empty box that had between 2 and 4 possible numbers, chose the first "guess" and selected that as an answer. If a contradiction was found, the guess was broken and the next "guess" for that same box was taken. I let the guess run through each box until a solution was found and the loop would break.</div><div class="separator" style="clear: both; text-align: left;"><br /></div><div class="separator" style="clear: both; text-align: left;">Unfortunately, this now works for 49/50 of the problems. In fact, it is problem 49 that my code fails to solve. After some frustration, I solved it by hand and submitted my solution which was correct. Maybe I'll return to this one later.</div>Andrewhttp://www.blogger.com/profile/16514776987162810586noreply@blogger.com0tag:blogger.com,1999:blog-3275671169173737245.post-21072192250722677662011-07-20T16:19:00.000-07:002011-07-20T16:19:34.585-07:00Ugh...So my file solves the first sudoku no problem, but gets stuck on the second one. Need to go through and see if my elimination method is not enough to complete this problem. I may need to add some guessing script to start guessing a number and basically brute forcing it from there... Frustrating.Andrewhttp://www.blogger.com/profile/16514776987162810586noreply@blogger.com0tag:blogger.com,1999:blog-3275671169173737245.post-44028731184209622742011-07-20T15:26:00.000-07:002011-07-21T13:56:19.944-07:00More problems on 96...So I have tried to get everything working on problem 96, but I seemed to run into some small error in my eliminations. By setting the eliminations to run until it finds the value of the first (top left) number, I get a 5 instead of 4. The solution puzzle it is giving me is slightly off. Need to go through and figure out which line is sending the wrong elimination out.<br /><br />Solution so far:<br /><br /> 5 0 3 9 2 1 6 7 4<br /> 9 0 4 3 7 5 8 2 1<br /> 2 7 1 8 0 6 4 0 0<br /> 0 0 8 1 0 2 9 0 0<br /> 7 0 9 5 0 4 1 0 8<br /> 0 0 6 7 0 8 2 0 0<br /> 0 0 2 6 8 9 5 0 0<br /> 8 0 0 2 5 3 7 0 9<br /> 6 9 5 4 1 7 3 8 2<br /><br />The zeros reflect unknown solutions.<br /><br />Edit:<br />So it turns out I had a typo and I was replacing the wrong zero with the solved number, go figure.Andrewhttp://www.blogger.com/profile/16514776987162810586noreply@blogger.com0tag:blogger.com,1999:blog-3275671169173737245.post-91972877506947886852011-07-20T00:08:00.000-07:002011-07-20T00:08:04.115-07:00Problem 96I decided today to try and conquer problem 96 and it's proved to be a bit of a challenge. I've spent maybe 40 minutes on it and have made pretty good progress.<br /><br />I figured the best thing to do was to read each puzzle number by number. By taking a solution matrix that was 81 x 9 (each row represents all of the possibilities of each square in the 9 x 9 puzzle) I replaced each possible number with a zero if it could not be a solution. So far I have only been able to eliminate each row and column that was already given a number from the .txt file. Now I'm going to figure out a way to eliminate each square from the given numbers, and then edit my loops a little bit to check when there is only 1 possible number left in each of the 81 spots. Finally, when there is only one number the process will repeat and eliminate these new choices vertically, horizontally and in squares.<br /><br />Solving the first puzzle given (doesn't solve all the way, obviously) takes about 0.04 seconds and I'm afraid that my code will be VERY slow for 50 problems. Maybe I can find a way to break when I finish the top left numbers instead of solving the entire puzzle.Andrewhttp://www.blogger.com/profile/16514776987162810586noreply@blogger.com0tag:blogger.com,1999:blog-3275671169173737245.post-11256143423555946522011-07-18T17:57:00.000-07:002011-07-20T12:00:25.368-07:00Site movedFor easier organization and with the discovery of a workaround for the lack of a comment box, I am moving everything over to <a href="https://sites.google.com/site/projecteulerwithmatlab/">Google sites.</a><br /><br />This should help a lot with ease of viewing and makes it so that I can have unlimited static sites. The url is:<br />https://sites.google.com/site/projecteulerwithmatlab/<br /><br />This blog will be used to update my status on the problems as I solve them. Might be helpful to some as I'll go over where I get stuck or potential problems that I find as I go about solving the problem.Andrewhttp://www.blogger.com/profile/16514776987162810586noreply@blogger.com0tag:blogger.com,1999:blog-3275671169173737245.post-32321127081489258182011-07-18T14:57:00.000-07:002011-07-18T14:57:49.010-07:00LayoutI'm still doing a lot of modifying to the general layout of this page. I'm currently organizing my posts in a chunking method that can be viewed to the right of the page. If you click the problem set you can choose each problem individually from there. Commenting is enabled so feel free to ask questions, give suggestions, or pose similar problems.<br /><br />Also, I'm open to suggestions for page layout and site mapping, so feel free to give any suggestions here.Andrewhttp://www.blogger.com/profile/16514776987162810586noreply@blogger.com0tag:blogger.com,1999:blog-3275671169173737245.post-27342277650679473932011-07-18T14:55:00.000-07:002011-07-18T14:56:07.491-07:00Welcome!<div style="color: #565555; font-family: Arial, sans-serif; font-size: 13px; line-height: 19px;">My name is Andrew, and I am a fourth year mechanical engineering student. I have very little experience with Matlab, but I recently started solving <a href="http://www.google.com/url?q=http%3A%2F%2Fwww.projecteuler.net&sa=D&sntz=1&usg=AFrqEzeoGfotQ1E3ltMTZiZGDCD-PCo5dA" style="color: #451670;" target="_blank">Project Euler</a> problems using Matlab. Unfortunately, whenever I need to look around for help I seem to only find sites dedicated to solving them in Python, Java, etc. Thus, I wanted to create a site in which I outline my thought pattern and code that I used in Matlab. I have no formal programming experience and I admit my code can be quite barbaric at times, but again this site is not for advanced Matlab users or computer scientists, just engineers hoping to get more practice with Matlab by solving fun little math challenges. Also, I would love for people to make recommendations or question parts of my code. Oftentimes my method of solving the problems is rarely the most elegant and I can use help in optimizing my code/approach.</div><div style="color: #565555; font-family: Arial, sans-serif; font-size: 13px; line-height: 19px;"><br /></div><div style="color: #565555; font-family: Arial, sans-serif; font-size: 13px; line-height: 19px;">Finally, I'm a complete novice when it comes to website design, so please bear with. If you have any recommendations to formatting or site mapping feel free to let me know and I'll try to get this site more visually appealing.</div>Andrewhttp://www.blogger.com/profile/16514776987162810586noreply@blogger.com0