How can I solve The Riddle of Nine 9's programmatically? [closed] - algorithm

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
How can I solve this riddle programmatically? Could someone help me with some pseudo-code or something?
Nine 9s
Combining nine 9's with any number of the operators +, -, *, /, (, ), what is the smallest positive integer that cannot be expressed?
Hints:
The answer isn't zero. You can express zero like this:
(9 - 9) * (9 + 9 + 9 + 9 + 9 + 9 + 9).
Also, zero isn't a positive integer.
The answer isn't one. You can express one like this:
9 - (9 * 9 - 9)/9 + 9 - 9 + 9 - 9
It's not a trick question.
Be sure to handle parentheses correctly.
Notes:
You cannot use exponentiation.
You cannot concatenate (for example,
put two 9's together to make 99).
The - operator can be used in either
its binary or unary form.
Assume base 10.
This is actually a famous puzzle and there are probably many solutions hovering around the internet. I am not sure if any of them is correct or not. Does anybody have a well explained solution?

The answer is 195, here is some Python code that simply builds up all possible expressions by forming new expressions from exp1 OP exp2. It runs in 0.165s on my PC.
exp = [set() for _ in xrange(10)]
exp[0].add(0)
exp[1].update([9, -9])
for i in xrange(1, 10):
for a in list(exp[i]):
for j in xrange(i, 10):
for b in list(exp[j-i]):
exp[j].update([a+b, a-b, a*b])
if b != 0:
exp[j].add(a/b)
n = 0
while n in exp[9]:
n += 1
print n
EDIT:
If the answers must be exact integers (and not just the rounded result of integer division) then a check must be done when division is done.
if ((b != 0) and ((a/b) == float(a)/b)):
exp[j].add(a/b)
Under this interpretation of the rules, the new answer is 138.
(the existing version computes 1386/10 [or -1386/-10] and gets 138)

195, http://members.iinet.net.au/~tmorrow/mathematics/ninenines/ninenines.html

Related

Float to whole integer keeping digits after decimal [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a float that has eight digits after the decimal. I want to convert this to a whole number/integer, keeping the digits after the decimal. For example: my float is 0.06870697, and I want it to be 006870697.
Everything I've tried so far removes all the digits after the decimal.
Just multiply it to shift the digits, then use a printf-style formatter:
v = 0.06870697
'%09d' % (v * 10e7)
# => "006870697"
Multiplying a value by 10e7 (10 x 107 = 108) shifts the decimal place eight digits to the right.
The below solution is solving problem for the number in the question. You can do as follows.
value = 0.06870697
value.to_s.delete('.')
=> "006870697"
Not as elegant as tadman's but below versions seems working -))
1.
a = 0.06870697
a.to_s.split(".").join("")
2.
a.to_s.gsub(/[^\d]/, '')
3.
a.to_s.slice! "."
Yet another option:
val = 0.06870697
num = (val * 10 ** 8).to_i.to_s
num.rjust(9, '0')
=> "006870697"
You can shift the decimal point eight digits to the right by multiplying by 10^8.
(0.06870697 * 10 ** 8).to_i
# => 6870697

Final result of repeatedly adding prime factors of a number and replacing that number by the sum until it repeats [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Consider the following operation -- we take a positive integer n and replace it with the sum of its prime factors (if a prime number is presented multiple times in the factorization of n, then it's counted the same number of times in the sum). This operation is applied sequentially first to the given number, than to the first result, than to the second result and so on, until the result remains the same.
Given any number, find the final result of the operation.
Example:
24 -> (2 + 2 + 2 + 3) = 9 -> (3 + 3) = 6 -> (2 + 3) = 5 -> 5.
So the answer for 24 is 5.
Other than a brute force solution i could not find a better solution
You may want to take a look the OEIS page for this sequence.
It includes a couple of code snippets, for example this one in Maple:
f:= proc(n) option remember;
if isprime(n) then n
else `procname`(add(x[1]*x[2], x = ifactors(n)[2]))
fi
end proc:
f(1):= 0: f(4):= 4:
map(f, [$1..100]); # Robert Israel, Apr 27 2015
I don't know Maple, but that looks a lot like a recursive definition like the one you're suggesting. I'd therefore be inclined to say that iteration until a prime is reached (excepting special cases for 1 and 4) is the most efficient method of calculation.
There's also a Mathematica script, but it's completely impenetrable to me:
ffi[x_] := Flatten[FactorInteger[x]] lf[x_] := Length[FactorInteger[x]] ba[x_] := Table[Part[ffi[x], 2*w-1], {w, 1, lf[x]}] ep[x_] := Table[Part[ffi[x], 2*w], {w, 1, lf[x]}] slog[x_] := slog[x_] := Apply[Plus, ba[x]*ep[x]] Table[FixedPoint[slog, w], {w, 1, 128}]
f[n_] := Plus ## Flatten[ Table[ #[[1]], {#[[2]]}] & /# FactorInteger#n]; Array[ FixedPoint[f, # ] &, 87] (* Robert G. Wilson v, Jan 18 2006 *)
fz[n_]:=Plus##(#[[1]]*#[[2]]&/#FactorInteger#n); Array[FixedPoint[fz, #]&, 1000] (* Zak Seidov, Mar 14 2011 *)
This related sequence gives the number of iterations required until a result is reached. It says there's only one value of n < 10000 for which more than 10 iterations are required, so it seems a quickly converging algorithm too.

Discrepancy in simplification [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 8 years ago.
Improve this question
For the below mentioned
simplify[(1/x)*(t/x)^(N-1)*(1-(t/x)^N)^(-2)]
and
simplify[(1/x)*(t/x)^(N-1)*(-1+(t/x)^N)^(-2)]
I am getting the same output which is
(t/x)^N/(t (-1 + (t/x)^N)^2)
How is it possible? Kindly help.
Thanks in advance.
The only terms that are different in your "code" are
(1-(t/x)^N)^(-2)
and
(-1+(t/x)^N)^(-2)
and these are identical too. It's because the parentheses that are raised to the power of -2 are negatives of each other. A power of something to -2 is equal to 1 over the power the same thing to 2 and, as you certainly know, x^2 is equal to (-x)^2.
Proof: just perform the exponentiation. Let B = (t/x)^N (so that I don't need to write it over and over again). The first expression:
(1 - B)^(-2) = 1 / ((1 - B)^2) = 1 / (1 - 2*B + B^2)
And the second expression:
(-1 + B)^(-2) = (B - 1)^(-2) = 1 / ((B - 1)^2) = 1 / (B^2 - 2*B + 1) = ...
... = 1 / (1 - 2*B + B^2)
Proved.
If we were to substitute back for the B then the result is
1 / ((t/x)^2N - 2*(t/x)^N + 1)

From point A to point B, can only move up and right, how many possible movements? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
A friend of mine has had a programming test yesterday. He passed the test anyway, but I'm curious about the answer.
Here is the test, supposed you are on point A and need to go to point B. You can only move up and right. How many possible movements do you have?
The options he remembered were 16, 64, 3125.
What is the answer and how to explain that?
Rotate your picture. Your problem consists in finding the number of paths from a particular vertex in the Pascal graph to the root vertex. Labelling the vertices at level n by the integers 0, 1, ..., n, then the number of paths from vertex k at level n is the binomial coefficient "n choose k". In your case this is vertex k=4 at level n=8, and "n choose k"=70.
I disagree with the commentators that this question has nothing to do with programming. It can be solved fairly easily with a simple recursive function:
def n_ways(x, y):
if x == 0 or y == 0:
# we are on the "edge", there is only one way to get there
return 1
else:
# the number of ways to get here equals the sum of the number of
# ways to get to the point directly below and the number of ways
# to get to the point directly left
return n_ways(x-1, y) + n_ways(x, y-1)
Let's try it out. Point B is x position 4, y position 4.
>>> n_ways(x=4, y=4)
70
We can also use our algorithm to generate the diagram below:
>>> for y in reversed(range(5)):
... for x in range(5):
... n = n_ways(x, y)
... print str(n).rjust(2),
... print
...
1 5 15 35 70
1 4 10 20 35
1 3 6 10 15
1 2 3 4 5
1 1 1 1 1
As Stéphane Laurent has already mentioned, you can easily find Pascal's Triangle hiding in this algorithm! :)

How to calculate the inverse factorial of a real number? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 12 years ago.
Improve this question
Is there some way to calculate the inverse factorials of real numbers?
For example - 1.5 ! = 1.32934039
Is there some way to obtain 1.5 back if I have the value 1.32934039?
I am trying
http://www.wolframalpha.com/input/?i=Gamma^(-1)[1.32934039]
but that is a fail.
Using wolframalpha.com, you can ask for
Solve[Gamma[x+1]==1.32934039,x]
As mentioned in the comments, Gamma does not have a unique inverse. True even when you are solving for a conventional factorial, e.g.
Solve[Gamma[x+1]==6,x]
yields several answers, of which one is 3.
Instead of using Gamma[] in WolframAlpha, you can also use Factorial[]:
Solve[Factorial[x]==6,x]
Solve[Factorial[x]==1.32934039,x]
David Cantrell gives a good approximation of Γ-1(n) on this page:
k = the positive zero of the digamma function, approximately 1.461632
c = Sqrt(2*pi)/e - Γ(k), approximately 0.036534
L(x) = ln((x+c)/Sqrt(2*pi))
W(x) = Lambert W function
ApproxInvGamma(x) = L(x) / W(L(x) / e) + 1/2
For integers you can do:
i = 2
n = someNum
while (n != 1):
n /= i
i += 1
return (i==1 ? i : None)
The factorial for real numbers has no inverse. You say that "each function must have an inverse". That is incorrect. Consider the constant function f(x)=0. What is f^-1(42)? For a function to be inverse it must be both an injection and a surjection.

Resources