6

Algorithm - Represents an integer by 4-digit expression

 2 years ago
source link: https://www.codesd.com/item/algorithm-represents-an-integer-by-4-digit-expression.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Algorithm - Represents an integer by 4-digit expression

advertisements

This is my problem: "I give you the 4 digits 1, 2, 3, 4. I want you to use all four of these digits to create mathematical expressions for number 1, 2, 3 etc. The winner will be the entry that produces for the longest range of number from 1 upwards. You can use the mathematical symbols + - * / . In addition brackets and square root are allowed. Please note that you can place the digits side by side to form a 2 or 3 digit number. Examples would be:

1 = ( 3/1 ) - ( 4/ 2 )

2 = ( 2 * 3 ) – ( 4/1)

3 = 21 / ( 3 + 4 )

4 = "

So I need an algorithm which:

    `Input: an integer`

    `Output: an expression formed by digits 1,2,3,4`

Right now I can only think of brute force algorithm:

First try the expression which formed by only one-digit number. In this expression, try all 4 operators +,-,*,/ in every possible position. Then try to put more bracket.

Then try try the expression which formed by two-digit number and two one digit number...

But implement this algorithm is hard because I cannot think of all the cases.

Anyone got better ideas?


May the expressions contain multiple occurrences of the digits?

If hat's ok, you only need expressions for the numbered 1 to 4. you can Compute every other Natural number by repeatedly adding 4 to One of the Base expressions.

If digits must occur precisely once, ignore for the time being parentheses and the square root. then you have expressions at most 7 slots in length: 4 digits (juxtaposed digits form a number) and at max 3 (Infix) Operators. That should be amenable to Exhaustive search.

example count for expressions with 7 slots:

(7 choose 4) * 4! * (4 choose 3) * 3!

4 positions out of 7, permute 4 digits on These positions, choose 3 out of 4 Operators, permute their sequence.

Parentheses add at most 6 slots: at most 1 pair for each Operator ( otherwise your expressions wouldn't be syntactically correct).

the square root may be positioned in Front of any Slot occupied by a digit, in Front of an Opening bracket and in Front of another Square Root. Note that Since each digit may occur only once, the Maximum number representable without Square Roots is bounded. As Roots Shrink any number > 1 , the maximum number of iterated Square Root Applications is bounded too ( 4 at max i think )


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK