The sequence of triangle numbers is generated by adding the natural numbers. So the 7^th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:
1, 3, 6, 10, 15, 21, 28, 36, 45, 55, …
Thus list the factors of the first seven triangle numbers:
Here you can see that 28 is the first triangle number to have over five divisors.
I need to write a program that takes a single argument on the command line. This argument must be a file name, which contains the input data. The program should output to standard out the value of the first triangle number to have a number of divisors over the value given in the input file.
I need help. Code is needed and if you can explain with an algorithm then it would be great.
Biman Ghosh wrote:Code is needed
That's too bad, because we are NotACodeMill.
and if you can explain with an algorithm then it would be great.
We will however help you find an algorithm yourself.
So let's start with asking what you can already do. Can you read the file? Can you find the (number of) divisors of a number? Can you determine if a number is a triangle number?
So...work on each part individually. Write a program that opens a file, reads the value, and converts it to an int. PROVE to yourself that it works by printing out that int.
Write another program that generates triangle numbers. For starters, have it print the first 10 or so. Write it in such a way that you can change how many to generate rather easily...possibly by having some kind of condition like "while i've not generated 10, generate the next". then later, you can change that to 20, or 100, or even 'while current number has less than 5 factors' and then 'while current number has less than X factors'...and perhaps X could be acquired from somewhere...like user input, or even from a file you read...
Write another program that finds all the factors of a number, and possibly counts them. Remember that you need to count DISTINCT factors...For example, 25 only has 3 factors: 1, 5, and 25. You don't want to count 5 twice. This may or may not be a problem, depending on your algorithm for finding factors.
Once you have all these pieces, you can start wiring them together.
The code is :
I am getting an error which says :
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at euler12.euler12.main(euler12.java:13)
Can you please help me correct the program?
So you can't access element 0 of the "args" array because it doesn't have any elements at all. Which would mean you forgot to pass any parameters when you ran the class from the command line.
Still, I'll let Wouter, Campbell or Fred decide whether or not to delete the contents.
I disagree. We learn little by reading code. we learn by writing our own, not by being given it.
Harsha Smith wrote:I request the moderators not to delete the code until the thread starter posts some reply. this is beginning java section. lnfact we learn a lot by reading code, writing a lot of code. Be it anyone. beginner or an expert.
I was busy at the time, otherwise I would have deleted it.