This question already has an answer here:
Closed 13 years ago.
Possible Duplicate:
Need help solving Project Euler problem 200
Similar to this question
Project Euler Problem 200.
I wrote up a brute force solution in Java that takes several hours to run, and produced the first 500+ sqube numbers, which I thought should be enough. However, none of the answers from 190 to 210 seems to be the correct answer.
I'm wondering what I'm doing wrong here and how I could optimize this. Could the problem lie in BigInteger.isProbablePrime()?
I'm not sure if Stackoverflow is the best place to ask this, but I seem to be stuck. I've included my code and the generated data.
I'd really appreciate it if someone would give me some hints or pointers.
Edit: I've run the program again simply with using the first 500,000 prime numbers; took a day to run but produced the correct answer.
I'm a Project Euler administrator. Please do not post information that can spoil the problem for others, particularly code and answers, even half-functioning code. Please edit your question accordingly. EDIT: Thank you for doing so!
It's not unusual for solvers to use the web to search for information on solving a problem, and it would take away some fun if they stumbled upon such a spoiler. (Yes, I know there are sites with lots of solutions ready-made, but at least they're generally for the lowered numbered easy problems.)
We have forums for discussing difficulties with problems and getting hints, which are aggressively edited for spoilers.
Aren't you supposed to think of a clever solution which doesn't take a day or even an hour to run? :D
I think the problem is isProbablePrime, which doesn't guarantee that a number is a prime. It just says that a prime which was found could be a prime with a certain probability.
You should use an algorithm which is certain that it has found a prime.
The first answer is incorrect because isProbablyPrime isn't always correct (hence the Probably). It's slow, in part, because you are using BigInteger. All the values involved will fit in a long. Why not use a long?
There may be some simple refactorings you could do that would save some time. It seems that the bulk of the time is spent in the nested for loop. The order of magnitude is n squared. You could reduce the complexity by not nesting loops. Another problem is that you are finding more potential results than are required. You only need to find 200. The reason you are needing to find more is due to the fact that you are not finding the potential results in their numeric order. The TreeSet does keep the results in order, but the algorithm would be faster if it were able to stop when the 200th result was found.
Related
I was working on an assignment today that basically asked us to write a Java program that checks if HTML syntax is valid in a text file. Pretty simple assignment, I did it very quickly, but in doing it so quickly I made it very convoluted (lots of loops and if statements). I know I can make it a lot simpler, and I will before turning it in, but Amid my procrastination, I started downloading plugins and seeing what information they could give me.
I downloaded two in particular that I'm curious about - CodeMetrics and MetricsReloaded. I was wondering what exactly these numbers that it generates correlate to. I saw one post that was semi-similar, and I read it as well as the linked articles, but I'm still having some trouble understanding a couple of things. Namely, what the first two columns (CogC and ev(G)), as well as some more clarification on the other two (iv(G) and v(G)), mean.
MetricsReloaded Method Metrics:
MetricsReloaded Class Metrics:
These previous numbers are from MetricsReloaded, but this other application, CodeMetrics, which also calculates cyclomatic complexity gives slightly different numbers. I was wondering how these numbers correlate and if someone could just give a brief general explanation of all this.
CodeMetrics Analysis Results:
My final question is about time complexity. My understanding of Cyclomatic complexity is that it is the number of possible paths of execution and that it is determined by the number of conditionals and how they are nested. It doesn't seem like it would, but does this correlate in any way to time complexity? And if so, is there a conversion between them that can be easily done? If not, is there a way in either of these plug-ins (or any other in IntelliJ) that can automate time complexity calculations?
Doing some practice coding problems in Java from Advent of Code and came across one where it asked me to find the smallest six-digit integer, combined with the leading string of iwrupvqb, that produced an MD5 hash that started with five zeros.
I found the answer to this part using the Apache DigestUtils.md5Hex function where I just brute forced through 100000-999999 and combined it with iwrupvqbuntil I got an MD5 that started with five zeros.
The answer came out to be iwrupvqb346386 creating the hash:
0000045c5e2b3911eb937d9d8c574f09
Now it's asking me to find one with six leading zeros. I've been going through pages and pages of how the md5 algorithm works, inverting MD5, etc but can't seem to figure out the problem in an equation format that would help me determine how the MD5 would be calculated based on the characters used.
I even let this loop run for like 30 mins - an hour to see if it got any hits outside of a six digit integer (because apparently there are none combined with this text phrase that create six leading zeros)
I don't really know anything about hexadecimal so at this point I'm just taking shots in the dark and trying to guess number combos all night isn't really my thing. I don't necessarily need to solve this problem for anything besides practice but I am curious to know more about what's going on here. (And yes I am aware that MD5 is compromised and I wouldn't ever use it in production)
This problem can only be solved by brute-forcing. That is exactly how "proof-of-work" in Bitcoin works, for example. The only way to speed it up is to optimize each step in your calculation. Bitcoin miners have moved to specialized hardware because of this. They don't do anything "special" or "clever", they just calculate hashes very, very fast.
You can only optimize the code and throw more/better hardware at it. A cluster of compute nodes would also work well here, the problem lends itself to parallel processing (again, Bitcoin mining pools).
If you have a multi-core CPU, an easy thing to do is to use one Thread per CPU. Should speed up linearly (which may still not be fast enough).
I'm also working through the Advent of Code but I'm using PowerShell. I solved Puzzle 2 of Day 4 with basically the same code that I used to solve Puzzle 1. The only change was to the WHILE condition to check for six leading zeroes. It did take a lot longer to run than it did to solve for Puzzle 1. I just kicked it off and went to bed after I saw this post. Got my answer when I woke up. My code is posted at Github. Advent of Code Day 4 Puzzle 2 solution with PowerShell
I was searching trough google for an answer but i can't find something usefull. I am making games with libGDX and till now performance was not a big issue. But the game that i am working on now, will need some more optimization so here is my question:
With libGDX there is a lot of floats. I know that int is faster but what if i cast floats into int? Is this faster then using a float numbers or should just go with floats?
I don't need very high precision, becouse i mostly use that numbers for coordinates. But with much multiplications (i don't use division becouse it is slower) though the code i wonder what i should use.
Without even seeing your code the answer is: you're probably looking in the wrong place. The performance problem is not due to the use of floats, and switching to ints will not fix it.
As soon as you get performance problems (and even before) you must find ways to benchmark and measure the performance of your code. You might use profiling, or internal tracing, or some other way but you must do it.
Most performance problems are due to bad algorithms and/or a few small functions called millions of times. Fix the algorithms and recode the small critical functions and you will probably solve the problem. Switching your code to int is something you do after you've fixed everything else and need just a little more.
Put up some code on CodeReview and I'm sure you'll get lots more help with it.
The problem - I have 10 number of cards value 1 to 10. Now I have to arrange the cards in away that adding 5 cards gives me 36 and product of remaining 5 cards give me 360.
I had successfully made a GA to solve cards Problem in java. Now I am thinking to solve same problem with Neural Network. Is it possible to solve this by NN? What approach should I take?
This problem is hard to solve directly with a Neural Network. Neural Networks are not going to have a concept of sum or product, so they won't be able to tell the difference between a valid and invalid solution directly.
If you created enough examples and labelled then then the neural network might be able to learn to tell the "good" and "bad" arrangements apart just by memorising them all. But it would be a very inefficient and inaccurate way of doing this, and it would be somewhat pointless - you'd have to have a separate program that knew how to solve the problem in order to create the data to train the neural network.
P.S. I think you are a bit lucky that you managed to get the GA to work as well - I suspect it only worked because the problem is small enough for the GA to try most of the possible solutions in the vicinity of the answer(s) and hence it stumbles upon a correct answer by chance before too long.
To follow up on #mikera's comments on why Neural Networks (NNs) might not be best for this task, it is useful to consider how NNs are usually used.
A NN is usually used in a supervised learning task. That is, the implementer provides many examples of input and the correct output that goes with that input. The NN then finds a general function which captures the provided input/output pairs and hopefully captures many other previously unseen input/output pairs as well.
In your problem you are solving a particular optimization, so there isn't much training to be done. There is just one (or more) right answers. So, NNs aren't really designed for such problems.
Note that the concept of not having a sum/product doesn't necessarily hurt a NN. You just have to create your own input layer which has sum and product features so that the NN can learn directly from these features. But, in this problem it won't help very much.
Note also that your problem is so small that even a naive enumeration of all combinations (10! = 3,628,800) of numbers should be achievable in a few seconds at most.
So I am in a Java class in school, and I have a relatively simple assignment that I just can't figure out. It's not a problem to research the answer, so I am throwing it out to the brightest people out there. My simple java program takes 4 numbers as input, and then it is just supposed to spit those numbers right back out, but in order smallest to biggest. Anyone have any thoughts? I know there are probably lot of different ways to do this, but I am sure this can be done simply. My teacher for example, said that he can do it using only 6 non-nested or else-using if statements! I think he got into my head a little and so I am getting hung up. Any help out there would be greatly appreciated. Thanks
Read up on bubble sort -- an unrolled implementation of bubble sort would solve this problem with exactly 6 comparisons, and is probably what your teacher has in mind.