programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# I need help to code this program

Greenhorn
Posts: 6
• 1
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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.