wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Euler's Triangle number program. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Euler Watch "Euler New topic
Author

Euler's Triangle number program.

Biman Ghosh
Greenhorn

Joined: Oct 04, 2011
Posts: 2

I need the code for the following program. I have tried many times but not getting it done.


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:

1: 1
3: 1,3
6: 1,2,3,6
10: 1,2,5,10
15: 1,3,5,15
21: 1,3,7,21
28: 1,2,4,7,14,28

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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19682
    
  20

Welcome to the Ranch!

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?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

One of the first things to learn is that programming jobs like this can be broken down into individual, discrete tasks. As Rob has implied, you need to figure out how to do each part by itself first, before you try and solve the whole problem.

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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Biman Ghosh
Greenhorn

Joined: Oct 04, 2011
Posts: 2

I have written the program and I am getting error but I can't correct it.
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?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

The first thing to do is to identify the line of code which threw the exception. At the end of your exception's text you see the number "13"... that means the error occurred at line 13. Which is this line, right?

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.
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
I have tried many times but not getting it done


You need code? here is the code that may work(not completely tested). if it works read and understand the code and then you'll be able to write algorithm on your own

John Jai
Bartender

Joined: May 31, 2011
Posts: 1776
here is the code
The code might stay till Campbell sees it...
Harsha Smith
Ranch Hand

Joined: Jul 18, 2011
Posts: 287
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.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19682
    
  20

But we should LetThemDoTheirOwnHomework. After all, we are NotACodeMill.

Still, I'll let Wouter, Campbell or Fred decide whether or not to delete the contents.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

Fred came, Fred saw, Fred deleted.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38496
    
  23
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 disagree. We learn little by reading code. we learn by writing our own, not by being given it.

I was busy at the time, otherwise I would have deleted it.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11256
    
  16

Campbell Ritchie wrote:I was busy at the time, otherwise I would have deleted it.
It is a rare day when I beat Campbell to anything.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
Biman, I think you should take Fred's advice to heart. It looks as though you are approaching this like a procedural program. Java is not procedural. Use classes and methods to break your problem into manageable chunks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Euler's Triangle number program.