One of my friend was asked this question in the interview.
You have 5 zeros. using these 5 zeros and any mathematical functions, you have to get the result of 120.
He could not answer this. Neither I am able to see any valid answers.
Does anyone have any solution to this?
( 0! + 0! + 0! + 0! + 0! ) ! = 120
(cos(0) + cos(0) + cos(0) + cos(0) + cos(0))!
I can do it with 4 zeros: ((0! + 0! + 0!)! - 0!)!
Use factorial
0! = 1
(0! + 0! + 0! + 0! + 0!)! = 120
I recently came across this beautiful approach to represent any number using one zero!
The explanation is as follows:
Consider a right-angled triangle with sides (1,1,√2). Thus,
√2 = Sec ( Tan-1( 0! ) ). Now, consider a another right-angled triangle with sides (1,√2, √3). Here, √3 = Sec ( Tan-1 (Sec ( Tan-1( 0! ) ) ) ). Extrapolating this idea futher, for any number x, we can represent √x using one 0 as, √x = Sec ( Tan-1 (…… Sec ( Tan-1 (0!)) ……)), where Sec ( Tan-1 ….) is taken x-1 times.
There you go, 120 can be represented as, √14400 = Sec ( Tan-1 (…… Sec ( Tan-1 (0!)) ……)), with Sec ( Tan-1 ….) taken 14399 times.
I would buy the pure solution by #Iarsman, but I bet they were looking for something like:
factorial(not(0)+not(0)+not(0)+not(0)+not(0))
If you want to show you really know mathematical functions better than the interviewer, state it in terms of http://en.wikipedia.org/wiki/Peano_axioms:
SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS0 = 120
Or if you want to be too clever by half:
0/0 = 120 [Yes, that's not good practice, but it's as justifiable as any other answer]
Or if you want to show that mathematicians are often also comfortable using programming operators in the right circumstance:
(!0+!0+!0+!0+!0)!
I admit when I first saw it I was confused, because mostly this sort of question assumes that by mathematical function you mean "plus times minus divide" and maybe exponentiation. And I agree factorials are the intended answer. I agree it's an amusing question, and maybe it's sour grapes, but I really don't see the point of distinguishing people who've seen too many of these "think of the trick" questions from those who haven't, which is what this question does. (OK, it also sorts out who's never heard of a factorial, but so would asking "what's a factorial". This just makes sure you get the middle ground.)
"Mathematical" functions, hmmm, what else? Chemical ones?
let
zeroes = [0,0,0,0,0]
five = length zeroes
in five*five*five - five
Turns out we do not even need the zeores:
let
zeroes = [[], [], [], [], []]
five = length zeroes
in five*five*five - five
Use Factorial .
fact(fact(0)+....+fact(0))
0^0*1111 = 1111 (2 zeroes)
1111000 = 120 in binary (remaining 3 zeroes used here)
(0!0!*0!0!)-0! =(11*11) -1 =121-1 =120 is how i solved in an interview and the interviewer was amazed :)
how about one zero :P
(((((0!)++)++)++)++)!
RoL(RoL(RoL(RoL(NOT(RoL(NOT(RoL(NOT(RoL(NOT(0)))))))))))
One zero strictly logical using Windows Programming Calc
Related
I have written this function to find indices based on certain criteria. It should work, the problem is that it will take 2-3 days to run on my pc. Is there any way to get it down below an hour (or faster at all) ? This really doesn't need to be very fast. But 2 days is unacceptably slow.
I don't expect an in depth analysis on the function (Though it would be nice). Just some general improvements.
All it essentially is is 3 for-loops used to populate 8 large 3d arrays using another 256x8 matrix Logic. Then a few logic tests to find the desired index.
%These are sample values from the g.u.i. and other functions -
%ignore up til the loops unless you need it to understand something in the loops.
PriceMat=[58867 55620 16682 97384 11660 18175 25896 16300];
CapMat=[1400 1200 450 3600 150 1330 2000 250];
RepMat=[58 53 31 127 15 164 242 27];
DesiredRep=293.04;
DesiredCap=2600;
prevmin=99999999;
P=perms(0:7);
D=zeros(256,8,40320);
Cap=zeros(size(D,3),8);
Rep=zeros(size(D,3),8);
Price=zeros(size(D,3),8);
SufRep=zeros(1,size(D,3));
SufCap=zeros(1,size(D,3));
CapTot=zeros(1,size(D,3));
RepTot=zeros(1,size(D,3));
PriceTot=zeros(1,size(D,3));
for i=1:40320
for x=1:8
for j=1:256
D(j,x,i)=P(i,x)*Logic(j,x);
Cap(i,x)=D(j,x,i)*CapMat(x);
Price(i,x)=D(j,x,i)*PriceMat(x);
Rep(i,x)=D(j,x,i)*RepMat(x);
CapTot=sum(Cap,2);
RepTot=sum(Rep,2);
PriceTot=sum(Price,2);
if CapTot(i)>=DesiredCap
SufCap(i)=true;
else
SufCap(i)=false;
end
if RepTot(i)>=DesiredRep
SufRep(i)=true;
else
SufRep(i)=false;
end
if SufRep(i)==true && SufCap(i)==true
if PriceTot(i)<=prevmin
prevmin=i;
end
end
end
end
end
return prevmin
use bsxfun it's so much FUN!
Here's how you can compute matrix D in a single line (no loops):
D = bsxfun( #times, permute( P, [3 2 1] ), Logic );
I guess you can take it from here and compute the rest of the matrices this way - no loops.
You said "it would be nice" to get some in depth analysis of your function. It's pretty complex -and can be greatly simplified. I am a little bit worried about the amount of memory that my solution would take - one of your 256x8x40320 arrays is about 660 MB, and I create four. If that's not a problem, great. Otherwise you might have to choose a more conservative data type to keep memory requirements down - if you start swapping to disk you are dead, timing wise.
So let's assume you are not limited by RAM, then the following will speed things up considerably (note - I am stealing Shai's suggestion to use bsxfun). Note also that I am clearing the "really big" arrays after taking their sum - this could all be done in one line but it would be even harder for you to follow:
D = bsxfun( #times, permute( P, [ 3 2 1] ), Logic );
Cap = bsxfun( #times, D, CapMat );
CapTot = sum( Cap, 2 );
clear Cap
Price = bsxfun( #times, PriceMat );
PriceTot = sum( Price, 2 );
clear Price
Rep = bsxfun( #times, D, RepMat ); % <<<<< STRONGLY recommend not to use RepMat -
% <<<<< to avoid confusion with built in repmat()
RepTot = sum( Rep, 2 );
clear Rep
CapRepOK = ( CapTot >= DesiredCap && RepTot >= DesiredRep ); % logical array - fast, small
[minPrice minPriceInd ] = min(PriceTot(CapRepOK)); % find minimum value and index
% convert index to correct value of `i` in original code:
cs = cumsum(ok(:)); % increases by one for every value that meets criteria
% but we need to know which original index that corresponds to:
possibleValues = find( cs == minPriceInd );
[j i] = ind2sub( size(CapRepOK), possibleValues(1) );
prevmin = i;
Obviously I don't have your data so it's a bit hard to be sure this replicates your functionality exactly - but I believe it does. If not - that's what comments are for.
I suspect it is possible never to create the largest arrays (D etc) with some careful thought - if you are truly memory starved that may be needed.
By reading the title it may sound like a silly question, but I have a data structures exam tomorrow and some formulas I need to know for algorithm analysis are read as (n – floor(log (n + 1)). What is the meaning of floor?
Thanks
floor(x) is the largest integer not greater than x. You can easily find this information on the web, here for example.
e.g.
floor(1.12) = 1
floor(0.53) = 0
floor(-3.4) = -4
One thing that can confuse people is the floor of a negative value. Some might initially think that floor(-3.4) is -3 when in reality it is -4 by the definition of floor(x).
As a note, floor(x) is often written as .
To round down to the nearest integer value.
For positive numbers: Remove the decimal portion. eg. floor(3.4): 3
For negative numbers: Remove the decimal portion and subtract one. eg. floor(-3.4): -3 - 1 = -4
Hope this helps.!
Wouldn't it be better to see an error when dividing an odd integer by 2 than an incorrect calculation?
Example in Ruby (I'm guessing its the same in other languages because ints and floats are common datatypes):
39 / 2 => 19
I get that the output isn't 19.5 because we're asking for the value of an integer divided by an integer, not a float (39.0) divided by an integer. My question is, if the limits of these datatypes inhibit it from calculating the correct value, why output the least correct value?
Correct = 19.5
Correct-ish = 20 (rounded up)
Least correct = 19
Wouldn't it be better to see an error?
Throwing an error would be usually be extremely counter-productive, and computationally inefficient in most languages.
And consider that this is often useful behaviour:
total_minutes = 563;
hours = total_minutes / 60;
minutes = total_minutes % 60;
Correct = 19.5
Correct-ish = 20 (rounded up)
Least correct = 19
Who said that 20 is more correct than 19?
Among other reasons to keep the following very useful relationship between the sibling operators of division and modulo.
Quotient: a / b = Q
Remainder: a % b = R
Awesome relationship: a = b*Q + R.
Also so that integer division by two returns the same result as a right shift by one bit and lots of other nice relationships.
But the secret, main reason is that C did it this way, and you simply don't argue with C!
If you divide through 2.0, you get the correct result in Ruby.
39 / 2.0 => 19.5
I stumbled upon this question:
7 power 7 is 823543. Which higher power of 7 ends with 823543 ?
How should I go about it ? The one I came up with is very slow, it keeps on multiplying by 7 and checks last 6 digits of the result for a match.
I tried with Lou's code:
int x=1;
for (int i=3;i<=100000000;i=i+4){
x=(x*7)%1000000;
System.out.println("i="+ i+" x= "+x);
if (x==823543){
System.out.println("Ans "+i);}
}
And CPU sounds like a pressure cooker but couldn't get the answer :(
Multiply modulo 10^6. See this Lua code.
local x=1
for i=1,100000 do
x=(x*7) % 1e6
if x==823543 then print(i) end
end
You could use Euler's generalization of Fermat's little theorem which applied to your case says that for any number a that is not divisible by two or five, a to the power 400000 is equal to 1 modulo 10^6. Which means that 7^400000 is equal to one and 7^400007 is equal to 823543 modulo 10^6
There may be smaller powers of 7 that are also equal to one modulo 10^6. Any such power should be a divisor of 400000. So if you search all divisors of 400000 you should find your answer.
Brute-force solution in Python:
def check():
i = 8
while True:
if str(7**i)[-6:] == "823543":
print i, 7**i
break
i += 1
if __name__ == "__main__":
check()
Runs in a tad more then 10 seconds on my machine:
$ time python 7\*\*7.py
5007 25461638709540284156782446957365168367138070393489656084508152816071765490828583739345420574947301301356529652113030016806506783009529977928336772622054260724106711204039012806363481521302203821096274017061906820115931889920385802499836705571461280700786627503189500663279772123190279763997339608040949194040289041117811256914511855302927500076094761237077649092849658261309060277197389760351907599243227298336309204635761799394324969277220810221310805265921431367291459357151617279190810954501590069774137519833706444943573459910208627100504003480684029216932299650285683013274883359754231186580602570771682084721896446416234857382909168309309630688331305154545352580787700878011742720440707156231891841057992434850068501355342227582074144717324718396296563918284728120322255330707786227631084119636101174217518654320128390401231343058708073280898554293777842571799775647325449392944570725467462072394864457569308219294304248413378339223195121800534783732295135735588409249690562213409520783181313960347723827308102920022860541043691808218543350580271593107019737918976365348051012746817678592727727988993175444584453532474156202438866838819565827414970029052602274354173178190323239427022953097424087683011937130778414189673555875258508014323428137406618951161046883845551087123412471364400737145434714864392224194773030522352601143771552489895838728148761974811275894561985163094852437703080985644653666048979901975905667811053289029958524703063742007291722490298429637413913574845245364780928447142275001431370017543206188428912106120676556219532197108435997375879569102044435752697298456147512203108094030745606163915437604076966518127099543894645297945324345093247636119593298654296614887389164509070158924404441687810434488061150620012547321097786493748417764592151734279632949607485719050349385098350202294648324398902047614892248381794929374952059877187100434970751833289677556040879755065563758085919673107576808662549999202791489324437408075089456174056904323973798979207791446889016369166632636035638123394649891606479407561222474471530411700646266636732205895085248823824764170316644547100628119484733814900100986786082211477261114056206393554335903410036064553032366200714266053598548735147707681592574886559888869327771461046450774938490837810526377213647071217152427693219479552580138352651791476758476864761332281826701978038126122728967682552206820425685782165630494478519812498630475776384700259524274670258777572341538755828794632819515842335609785884327007667337426644594091547392441314523035569100326662245947022517857248412004291423280879791576077952474202068318934524092750814844945529148131063116233331840380254781283689084385600858175504170157015630699919186013526052643206240745256569669847298952477441594748635701081031979500954081732722211598460098426985932512920424237248250698541558227081975966598720056015879151923686438360541128221854058867910136449528237543680180470919685862102358708465872395643586424250239281775923511452769821487580471289910257908740451431952197725174728917413539539795856895884961513784804247268727165303942024508367184898248006123651950710237279288601317817391869969699767431782664773248447758526620050588927086506013616563459173620496200348863132442180734592661348887012997849309740799709045762939781801481205704629203758859772476278892928066844445088880207986848424855774325574728566649552154520262460969975214802828263093097997124519153537792591659204109087699977445745067857471581656151077039286563447099850537157044829081400190710358959493358343935904669416958301921942118288210835104022359479660409954097409669785908666166908117346073702337825511531650740900904200220658196171839969860945908503151878488455004283026700303698398069644419655035582904253655945381261383285097911378914794161551292914993411444083214513058414480129560671193659591364146612550890288116403596333209446976453193340267725222134755872075133141618388704912211996423838163706006930973361661094103734887312836613195349528793780496172839376426055357343094188450140671138356505144988151110902047791487250988374130384058324229250761311655685931891857894126054047458969174494155762486464149775147410127618088224310828566286409277000561087588768230619606746804073498788244935099280434916850444895829823543
real 0m10.779s
user 0m10.709s
sys 0m0.024s
Not so much an answer, more a hint:
Observe that the pattern of rightmost digits of powers of 7 goes 1,7,9,3,1,7,9,3,1,7,... so you only need to generate every 4th power of 7 from the 3rd. Further study might show a pattern for the two (three, four, ...) rightmost digits, but I haven't done studied them for you.
Be prepared for some very large numbers, Mathematica reports that the next power of 7 with the sought-for rightmost digits is the 5007th.
Which I guess answers your question -- a faster approach is to post on SO and wait for someone to tell you the answer ! You could even try Wolfram Alpha if you don't like the SO algorithm.
The Fermat's little theorem approach is a mathematically sensible one, and just mulitplying over and over by 7 mod 10^6 is the simplest code, but there's another approach you could take that is computationally efficient (but requires more complex code). First, note that when multiplying by 7 the last digit depends only on the last digit before (i.e. we're doing everything mod 10). We multiply repeatedly by 7 to get
7 (4)9 (6)3 (2)1 (0)7 ...
Okay, great, so if we want a 3, we start at 7^3 and go up every 7^4 from there. Now, we note that when multiplying by 7^4, the last two digits depend only on the last two digits of 7^4 and the last two digits of the previous answer. 7^4 is 2401. So in fact the last two digits will always be the same when going up by 7^4.
What about the last three? Well, 7^3 = 343 and 7^4 ends with 401, so mod 1000 we get
343 543 743 943 143 343
We've got our first three digits in column #2 (543), and we see that the the sequence repeats ever 5, so we should go up from there by 7^20.
We can play this trick over and over again: find how often the next block of digits repeats, find the right subsequence within that block, and then multiply up not by 7 but by 7^n.
What we're really doing is finding a (multiplicative) ring over the m'th digit, and then multiplying the sizes of all the rings together to get the span between successive powers that have the same N digits if we follow this method. Here's some Scala code (2.8.0 Beta1) that does just this:
def powRing(bigmod: BigInt, checkmod: BigInt, mul: BigInt) = {
val powers = Stream.iterate(1:BigInt)(i => (i*mul)%bigmod)
powers.take( 2+powers.tail.indexWhere(_ % checkmod == 1) ).toList
}
def ringSeq(digits: Int, mod: BigInt, mul: BigInt): List[(BigInt,List[BigInt])] = {
if (digits<=1) List( (10:BigInt , powRing(mod,10,mul)) )
else {
val prevSeq = ringSeq(digits-1, mod, mul)
val prevRing = prevSeq.head
val nextRing = powRing(mod,prevRing._1*10,prevRing._2.last)
(prevRing._1*10 , nextRing) :: prevSeq
}
}
def interval(digits: Int, mul: Int) = {
val ring = ringSeq(digits, List.fill(digits)(10:BigInt).reduceLeft(_*_), mul)
(1L /: ring)((p,r) => p * (r._2.length-1))
}
So, if we've found one case of the digits that we want, we can now find all of them by finding the size of the appropriate ring. In our case, with 6 digits (i.e. mod 10^6) and base 7, we find a repeat size of:
scala> interval(6,7)
res0: Long = 5000
So, we've got our answer! 7^7 is the first, 7^5007 is the second, 7^10007 is the third, etc..
Since this is generic, we can try other answers...11^11 = 285311670611 (an 8 digit number). Let's look at the interval:
scala> interval(12,11)
res1: Long = 50000000000
So, this tells us that 11^50000000007 is the next number after 11^11 with the same initial set of 12 digits. Check by hand if you're curious!
Let's also check with 3^3--what's the next power of 3 whose decimal expansion ends with 27?
scala> interval(2,3)
res2: Long = 20
Should be 3^23. Checking:
scala> List.fill(23)(3L).reduceLeft((l,r) => {println(l*r) ; l*r})
9
27
81
243
729
2187
6561
19683
59049
177147
531441
1594323
4782969
14348907
43046721
129140163
387420489
1162261467
3486784401
10460353203
31381059609
94143178827
Yup!
(Switched code in edits to use BigInt so it could handle arbitrary numbers of digits. The code doesn't detect degenerate cases, though, so make sure you use a prime for the power....)
Another hint: You are only interested in the last N digits: you can perform calculations modulo 10^N and keep the result fit nicely into an integer
From ProjectEuler.net:
Prob 76: How many different ways can one hundred be written as a sum of at least two positive integers?
I have no idea how to start this...any points in the right direction or help? I'm not looking for how to do it but some hints on how to do it.
For example 5 can be written like:
4 + 1
3 + 2
3 + 1 + 1
2 + 2 + 1
2 + 1 + 1 + 1
1 + 1 + 1 + 1 + 1
So 6 possibilities total.
Partition Numbers (or Partition Functions) are the key to this one.
Problems like these are usually easier if you start at the bottom and work your way up to see if you can detect any patterns.
P(1) = 1 = {1}
P(2) = 2 = {[2], [1 + 1]}
P(3) = 3 = {[3], [2 + 1], [1 + 1 + 1]}
P(4) = 5 = {[4], [3 + 1], [2 + 2], [2 + 1 + 1], [1 + 1 + 1 + 1]}
P(5) = 7 ...
P(6) = 11 ...
P(7) = 15 ...
P(8) = 22 ...
P(9) = 30 ...
Hint: See if you can build P(N) up from some combination of the results prior to P(N).
The solution can be found using a chopping algorithm.
Use for example the 6. Then we have:
6
5+1
4+2
3+3
but we are not finished yet.
If we take the 5+1, and consider the +1 part as finished, because all other ending combinations are handled by the 4+2 and 3+3. So we need to apply the same trick to the 5.
4+1+1
3+2+1
And we can continue. But not mindlessly. Because for example 4+2 produces 3+1+2 and 2+2+2. But we don't want the 3+1+2 because we will have a 3+2+1. So we only use all productions of 4 where the lowest number is greater or equal than 2.
6
5+1
4+1+1
3+1+1+1
2+1+1+1+1
1+1+1+1+1+1
2+2+1+1
3+2+1
4+2
2+2+2
3+3
Next step is to put this in an algorithm.
Ok we need a recursive function that takes two parameters. The number to be chopped and the minimal value:
func CountCombinations(Number, Minimal)
temp = 1
if Number<=1 then return 1
for i = 1 to Floor(Number/2)
if i>=Minimal then
temp := temp + CountCombinations(Number-i, i)
end for
return temp
end func
To check the algorithm:
C(6,1) = 1 + C(5,1) + C(4,2) + C(3,3) = 11, which is correct.
C(5,1) = 1 + C(4,1) + C(3,2) = 7
C(4,1) = 1 + C(3,1) + C(2,2) = 5
C(3,1) = 1 + C(2,1) = 3
C(2,1) = 1 + C(1,1) = 2
C(1,1) = 1
C(2,2) = 1
C(3,2) = 1
C(4,2) = 1 + C(2,2) = 2
C(3,3) = 1
By the way, the number of combinations of 100:
CC(100) = 190569292
CC(100) = 190569291 (if we don't take into account 100 + 0)
A good way to approach these is not get fixated on the '100' but try to consider what the difference between totalling for a sum n and n+1 would be, by looking for patterns as n increases 1,2,3....
I'd have a go now but I have work to do :)
Like most problems in Project Euler with big numbers, the best way to think about them is not to get stumped with that huge upper bound, and think of the problem in smaller terms, and gradually work your way up. Maybe, on the way you'll recognize a pattern, or learn enough to get you to the answer easily.
The only other hint I think I can give you without spoiling your epiphany is the word 'partition'.
Once you've figured that out, you'll have it in no time :)
one approach is to think recursive function: find permutations of a numeric series drawn from the positive integers (duplicates allowed) that add up to 100
the zero is 1, i.e. for the number 1 there are zero solutions
the unit is 2, i.e for the number 2 there is only one solution
another approach is to think generative function: start with the zero and find permutation series up to the target, keeping a map/hash or the intermediate values and counts
you can iterate up from 1, or recurse down from 100; you'll get the same answer either way. At each point you could (for a naive solution) generate all permutations of the series of positive integers counting up to the target number minus 1, and count only those that add up to the target number
good luck!
Notice: My maths is a bit rusty but hopefully this will help...
You are going well with your break down of the problem.
Think Generally:
A number n can be written as (n-1)+1 or (n-2)+2
You generalise this to (n-m)+m
Remember that the above also applies to all numbers (including m)
So the idea is to find the first set (lets say 5 = (5-1)+1) and then treat (5-1) as a new n...5 = 4 +1...5 = ((4-1)+1)+1. The once that is exhausted begin again on 5 = 3 + 2....which becomes 5 = ((3-1)+1)+2 ....= 2+1+2....breaking down each one as you go along.
Many math problems can be solved by induction.
You know the answer for a specific value, and you can find the answer for every value, if you find something that link n with n+1.
For example in your case you know that the answer for How many different ways can one be written as a sum of at least two positive integers? is just 1.
What do I mean with the link between n and n+1? Well I mean exactly that you must find a formula that, provide you know the answer for n, you will find the answer for n+1. Then, calling recursively that formula, you'll know the answer and you've done (note: this is just the mathematical part of it, in real life you might find that this approach would gave you something too slow to be practical, so you've not done yet - in this case I think you will be done).
Now, suppose that you know n can be written as a sum of at least two positive integers? in k different ways, one of which would be:
n=a1+a2+a3+...am (this sum has m terms)
What can you say about n+1? Since you would like just hints I'm not writing the solution here, but just what follows. Surely you have the same k different ways, but in each of them there will be the +1 term, one of which would be:
n+1=a1+a2+a3+...am+1 (this sum has m+1 terms)
Then, of course, you will have other k possibilities, such those in which the last term of each sum is not the same, but it will be increased by one, like:
n+1=a1+a2+a3+...(am+1) (this sum has m terms)
Thus there are at least 2k ways of writing n+1 as a sum of at least two positive integers. Well, there are other ones. Find it out, if you can :-)
And enjoy :-))