Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Something for the weekend 5

Status
Not open for further replies.

vgulielmus

Programmer
Jan 27, 2014
522
RO
Encode the followings formulas. The solutions must not include (explicit) mathematical formulas.
Code:
a) The factorial of 6
6! = 1*2*3*4*5*6
b) 6 to the power 4
6**4 = 6^4 = 6*6*6*6
c) The fifth root of 3125
3125^0.2 = 5
d) This math expression
6^3/2 = 108

* This is only for fun, not recommended as a substitute for math formulas.

Respectfully,
Vilhelm-Ion Praisach
Resita, Romania
 
Let's start ith a little fun answer.

* a) factorial of 6
o = CreateObject("internetexplorer.application")
o.navigate2("https"+"://o.visible = .T.

* b) 6 to the power of 4
o = CreateObject("internetexplorer.application")
o.navigate2("https"+"://o.visible = .T.

* c) The fifth root of 3125
o = CreateObject("internetexplorer.application")
o.navigate2("https"+"://o.visible = .T.

* d) 6^3/2 = 108
o = CreateObject("internetexplorer.application")
o.navigate2("https"+"://o.visible = .T.

*The splitting of URIs is needed to hide URIs within the spoiler section.
 
Awesome!
I was sure that must be more solutions.
Mine is totally different.

Respectfully,
Vilhelm-Ion Praisach
Resita, Romania
 
A different approach:

loXL = CREATEOBJECT("excel.application")
? "Factorial 6 = " + TRANSFORM(loXL.WorksheetFunction.Fact(6))
? "6 to the power of 4 = " + TRANSFORM(loXL.WorksheetFunction.Power(6,4))
? "The fifth root of 3125 = " + TRANSFORM(loXL.WorksheetFunction.Power(3125, 0.2))
? "6 cubed and halved = " + ;
TRANSFORM(loXL.WorksheetFunction.Product(loXL.WorksheetFunction.Power(6,3), 0.5))

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Great!
Yet another solution!

Respectfully,
Vilhelm-Ion Praisach
Resita, Romania
 
Surely are more solutions, because I have in my mind a totally different one (pure VFP)

Respectfully,
Vilhelm-Ion Praisach
Resita, Romania
 
Maybe you expand on your condition "must not include (explicit) mathematical formulas". That allows implicit, eg reformulating the original formula with other functions.
Then I'd do this with a few function definitions:

? Factorial(6)
? XtoThePowerOfY(6,4)
? NthRootOfX(3125,5)
? DivideBy2(XtoThePowerOfY(6,3))

Function XtoThePowerOfY(x,y)
Return Exp(Log(x)*y)
EndFunc
* Both the last and the next function make use of the law log(x^y) = log(x)*y.
* and of course exp(log(x^y)) = x^y
Function NthRootOfX(x,n)
Return Exp(Log(x)/n)
EndFunc

Function Factorial(n)
If n = 1
Return 1
Endif
Return n*Factorial(n-1)
EndFunc

Function DivideBy2(n)
Return BitRshift(n,1)
Endfunc

Bye, Olaf.
 
I have in my mind a totally different one (pure VFP)

I'm still thinking about it.

Just to be clear, when you say the solution must "not include (explicit) mathematical formulas", do you mean it must not include any of the arithmetic operators, specifically + - * / ^ ? What about built-in functions in LOG() and EXP()? Are those also disallowed?

Mike



__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
I like to see many solutions for the same problem. I appreciate any solution, because me and others, have much to learn from them.
But yours first approaches were closer to my intention.
Obviously, is hard to avoid the asterisk character, but used not as a math character (* for multiplication or ** for power)

If my own solution wouldn't be revealed by any other member, I will post it tomorrow.

Respectfully,
Vilhelm-Ion Praisach
Resita, Romania
 
So many different solutions, until now.
Honestly, I didn't expected.
I'm excited!


Respectfully,
Vilhelm-Ion Praisach
Resita, Romania
 
Ah, I see...

_CALCVALUE = 0
Activate Window "Calculator"
Clear Typeahead
Keyboard "6*5*4*3*2="

*...and so on

Bye, Olaf.
 
to the power of 4...

_CALCVALUE = 0
Activate Window "Calculator"
Clear Typeahead
Keyboard "6**="

Bye, Olaf.
 
This is the reason I like these weekend jokes so much.
They are like a brainstorming !
I really miss the possibility to give more than a single star :)

But... there is at least one more solution ;-)

Respectfully,
Vilhelm-Ion Praisach
Resita, Romania
 
Except the LOG() and EXP() approach, none of these solutions had crossed my mind.
VFp is an extremely versatile tool :)

Respectfully,
Vilhelm-Ion Praisach
Resita, Romania
 
Types of solutions, until now :
1) Automating other applications
2) Using alternative math functions
3) Using preprocessor directive
4) Using VFP's built-in calculator

Respectfully,
Vilhelm-Ion Praisach
Resita, Romania
 
One hint about my solution: it has something in common with Mike Lewis' solution.

Respectfully,
Vilhelm-Ion Praisach
Resita, Romania
 
Well, now I looked into that and yes, yet another idea. Normally I'd use it as follows:

Local loExcel, loSheet
loExcel = CreateObject("Excel.Application")
loExcel.Workbooks.Add()
loSheet = loExcel.ActiveSheet
loSheet.Cells(1,1).Value="6"
loSheet.Cells(1,2).Formula="=FACT(A1)"
loSheet.Cells(1,3).Formula="=A1^4"
loSheet.Cells(1,4).Formula="=3125^.2"
loSheet.Cells(1,5).Formula="=A1^3/2"
loExcel.Visible = .T.

Bye, Olaf.
 
This reminds me that when I was teaching an advanced VFP course, I asked the trainees to write a program that calculates mortgage repayments, without having to know the mathematics involved. I wanted them to use Excel automation, and the WorkSheetFunction object.

After I showed them the solution, one of them popped up and said he could do the whole think in one line of code, using VFP's built-in PAYMENT() function. I didn't even know that existed, which was slightly embarrassing.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Here are my solutions. They are a little less spectacular than the ones already proposed.
There are two small variations, but the main idea is the same.

I'm pretty sure other solutions are possible.

Code:
****************************
* 1) Evaluate(TransForm())
* Plus Chrtran, for factorial
****************************
*b) 6 to the power 4
? EVALUATE(TRANSFORM(64,"9^9"))
*c) The fifth root of 3125
? EVALUATE(TRANSFORM(31250.2,"9999^9.9"))
*d) This math expression
? EVALUATE(TRANSFORM(632,"9^9/9"))
*a) The factorial of 6
? EVALUATE(CHRTRAN(TRANSFORM(123456,"9,9,9,9,9,9"),',','*'))

****************************
* 2) Evaluate(TransForm())
* Plus SET SEPARATOR / SET POINT
****************************
*a) The factorial of 6
SET SEPARATOR TO '*'
? EVALUATE(TRANSFORM(123456,"9,9,9,9,9,9"))
*b) 6 to the power 4
SET SEPARATOR TO '*'
? EVALUATE(TRANSFORM(64,"9,,9"))
* or
SET SEPARATOR TO '^'
? EVALUATE(TRANSFORM(64,"9,9"))
*c) The fifth root of 3125
SET SEPARATOR TO '*'
? EVALUATE(TRANSFORM(31250.2,"9999,,9.9"))
* or
SET SEPARATOR TO '^'
? EVALUATE(TRANSFORM(31250.2,"9999,9.9"))
*d) This math expression
SET SEPARATOR TO '^'
SET POINT TO '/'
? EVALUATE(TRANSFORM(63.2,"9,9.9"))

SET SEPARATOR TO 
SET POINT TO

Respectfully,
Vilhelm-Ion Praisach
Resita, Romania
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top