• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Euler's Triangle number program.

 
Greenhorn
Posts: 2
C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Biman Ghosh
Greenhorn
Posts: 2
C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 28175
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

here is the code

The code might stay till Campbell sees it...
 
Harsha Smith
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Fred came, Fred saw, Fred deleted.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
She's out of the country right now, toppling an unauthorized dictatorship. Please leave a message with this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic