• Post Reply Bookmark Topic Watch Topic
  • New Topic

I need help to code this program  RSS feed

 
Greenhorn
Posts: 6
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is seen that the number, 125874 and its double 251748, contain exactly the same digits, but in a different order. Write a java program to find and print the smallest positive integer, x such that 2x, 3x, 4x, 5x and 6x contain the same digits.

This is the question. I tried to do only for 2x numbers. But very hard to do it.




This is the code I tried. Get multiple, sum and length. But some numerics such as 225, 225*2=450
This also get the same multiplication, sum and number length. But they are not permuted.


I only want this with loops.
 
Marshal
Posts: 4054
241
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

It's always a good idea to Use Code Tags when posting code to make it easy to read. I added them for you this time.
 
Kasun Nirmala
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Cooke wrote:Welcome to the Ranch!

It's always a good idea to Use Code Tags when posting code to make it easy to read. I added them for you this time.


I am very much new to this forum. Thank you for informing me.
 
Greenhorn
Posts: 7
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is as possible approach to find out if two integers have permuted digits. It uses two helper static methods. The digits() method counts the number of occurrences of each digit in a given integer.
Then the permut() method decides if two integers have the same number of occurrences of each digit. This should do the job.


 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dominique Ubersfeld, please note this, which is written on the title page of this forum:-
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
We believe it doesn't help learning to provide a complete answer; indeed without explanation it may harm the learning process. Don't be annoyed with me, but I have pulled rank and removed the complete solution.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kasun Nirmala, welcome to the Ranch again.
Please always tell us where such questions come from to avoid copyright problems and to give us a chance to assess the source for ourselves. We help you to learn, and don't supply complete solutions.
That looks like something out of Project Euler. Those exercises are to test your mathematical abilities more than your programming abilities. You would need to work out an algorithm for testing whether two numbers have the same digits in different order, e.g. 123 321. You would also need to work out how to count up. You shou‍ld not at this stage say anything about loops; that is an implementation detail which only comes later in the process.
Experience will give you a nice simple rule: no algorithm, no code. Until you have thought what the algorithm is, you can't create the code. Once you have the algorithm, the code will be relatively simple to create.
 
Kasun Nirmala
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dominique Ubersfeld wrote:Here is as possible approach to find out if two integers have permuted digits. It uses two helper static methods. The digits() method counts the number of occurrences of each digit in a given integer.
Then the permut() method decides if two integers have the same number of occurrences of each digit. This should do the job.




Excuse me?
 
Kasun Nirmala
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Kasun Nirmala, welcome to the Ranch again.
Please always tell us where such questions come from . . .


Actually I got it from my assignment. I have tried about 3 days now to overcome this problem. Tried many many ways. But invain. I couldn't overcome with any method. So, then I decided to ask from this forum.
Please help me on this. Can you please explain any algorithm to check the permute number?
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Start by asking your instructor. If that is a basic programming assignment, I am starting to worry about whether it isn't unreasonably difficult. If Project Euler is about maths rather than programming then maybe that assignment is about maths rather than programming. Also ask your fellow‑students for advice, remembering there is a limit to how much you are allowed to tell one another. Had I not deleted a complete solution and had you used it, your plagiarism software would have found that and you would have got a mark of 0.
You will have to work with pencil and paper to create the algorithms. I suggest you start with a method for checking how many of each digit a number contains. You would do well to tell us some ideas. Even wrong ideas. Simply writing them down will help you think. Forget about the equality part for the time being. You can do that later.

Please don't quote the whole of a preceding post; that simply makes the th‍read longer and longer without adding any new information; I shall remove part of the quote.
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Those exercises are to test your mathematical abilities more than your programming abilities. You would need to work out an algorithm for testing whether two numbers have the same digits in different order, e.g. 123 321. You would also need to work out how to count up.


Kasun Nirmala wrote:
Actually I got it from my assignment. I have tried about 3 days now to overcome this problem. Tried many many ways. But invain. I couldn't overcome with any method.

Can you please explain any algorithm to check the permute number?


First, I have to admit that I absolutely love solving mathematical algorithms. There is just something really elegant about a math solution versus a brute force solution.

... on the other hand, the pragmatic side of me says "how about getting to the solution first"? meaning do the brute force solution (for the comparison), finish the rest of the assignment; and then when you know that you have enough time, refactor the comparison algorithm. 

Anyway, a possible brute force solution would be to break the number into an array of digits, reorder the digits from low to high (ie. sort it), and then, just iterate comparing one digit at a time.

Henry
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:. . . a possible brute force solution would be to break the number into an array of digits, reorder the digits from low to high (ie. sort it), and then, just iterate comparing one digit at a time.

Henry
Is that really brute force?
Anyway, Kasun Nirmala, that is a possible suggestion. I can see three stages to that process, which means you can implement it as three parts (or maybe even four). The more parts, the better.
 
Kasun Nirmala
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Anyway, Kasun Nirmala, that is a possible suggestion. I can see three stages to that ...


Can you please explain those stages simply? The keys of them? Then I can try with them.
 
Kasun Nirmala
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:

First, I have to admit that I absolutely love solving mathematical algorithms. There is just something really elegant about a math solution versus a brute force solution...


Hmm.. I don't get it exactly.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kasun Nirmala wrote:. . . Can you please explain those stages simply? The keys of them? Then I can try with them.
Look at Henry's post and read the three stages in there.

There is a limit to how much help we can give; you shou‍ld however read all the hints you are given.
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:. . . I absolutely love solving mathematical algorithms. There is just something really elegant about a math solution versus a brute force solution. . . . Henry
That is why I thought it was a Project Euler problem.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!