Math Is Fun Forum

  Discussion about math, puzzles, games and fun.   Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫ • π ƒ -¹ ² ³ °

You are not logged in.

#1 2009-03-06 10:52:48

dirbax
Member
Registered: 2009-03-06
Posts: 2

C++ to mathematics

Hi , is it possible to transform the following code to math

int F( int n )
{
int r=0;
int mid=n/2;
for( int i=0 ; i<n ; i++)
{
if( i<mid) r+=1 ;
if( i==mid) r*=2 ;
if( i>mid) r=pow(r,3) ;
}
return r ;
}

and is this correct ? www.dirbax.com/inventions/dimd-dynamic-intelligent-mathematical-design-/practical-example-1.html

thanks

Offline

#2 2009-03-07 03:57:01

Onyx
Member
Registered: 2009-02-24
Posts: 48

Re: C++ to mathematics

Hi, you have a function f, taking a single argument, n, and returning a number r, so f(n)=r.

The main problem is the for loop. within the loop you could consider a seperate function:

This reasoning is ok in c, where we write for example r=r+1; to mean take the value stored in the memory location of r, add 1 to it, and save the new value in the original memory location, but writing the equality r=r+1 in mathematics cannot be true for any

.

Also while loops can be used to calculate finite series using

and
and can be readily compared in these cases, something like what you have is not so easy to compare. Usually it's a case of knowing the maths before implementing it as code, rather than trying to extrapolate mathematical expressions from the code.

Last edited by Onyx (2009-03-07 03:57:40)

Offline

#3 2009-03-09 00:34:18

TheDude
Member
Registered: 2007-10-23
Posts: 361

Re: C++ to mathematics

It's actually not too hard to turn that code into a mathematical function.  For now let's assume that n is even.  Starting at 0 and going up to n/2 - 1 we simply increment r, so we'll get (n/2 - 1) + 1 = n/2.  We then reach the next stage which is r *= 2, so we get r = n/2 * 2 = n.  We then raise r to the third power n/2 - 1 times, so we end up with

.

To see if this works try n = 6.  Using the function I came up with you get 6^9 = 10,077,696.  If we step through the C++ code we get this:

Odd numbers would be different and would depend on how n/2 is converted to an integer.  I believe most or all C++ compilers truncate any decimals, which would make the function

for odd n.


Wrap it in bacon

Offline

#4 2009-03-09 11:02:34

dirbax
Member
Registered: 2009-03-06
Posts: 2

Re: C++ to mathematics

Thanks guys

Is this mathematically correct ?

F(n)=I(0,n,n/2)   ( does it have the same behavious as the previous function in c++ ?)

with

I(r,i,m)  =I(r,i-1,m) +R(r,i,m)   and  I(r,0,m)=R(r,0,m)
R(r,i,m)= R1( r , i , m) + R2( R1( r , i , m) , i , m) + R3( R2( R1( r , i , m) , i  , m) , i , m)
R1(r, i , n) = r +  ( ifl( i , m) * 1 )
R2( r , i , n ) = ( r *  ife( i , m) ) + ( not(ife( i , m) * r )
R3( r , i,  n) = ( (r^2) *  ifg( i , m) ) + ( not(ifg( i , m) * r )
-----------
isz (x) = floor( 1 / (abs(x) +1) )
Out :   1 if x=0  ,  0 if  x <> 0
-----
sgn(x) = abs(x) / ( x + isz(x) )
Out :   1 if x>0  ,  0 if  x = 0 , -1 if x < 0
----
NOT (x) =  isz (x)
----
cmp(a,b) = sgn(a-b)
Out :   1 if a>b  ,  0 if  a=b, -1 if a<b
-----
isnz(x) = abs( sgn(x) )
Out :   1 if a<>0  ,  0 if  a=0
---
ife(a,b) = isz(a-b)
Out :   1  if a=b  ,  0 if a<>b
--------------------
ifg(a,b) = isz(cmp(a,b)-1)
Out :   1  if a>b  ,  0 if a<b
--------
ifl(a,b) = isz(cmp(a,b)+1)
Out :   1  if a<b  ,  0 if a>b



Source  : dirbax.com/inventions/dimd-dynamic-intelligent-mathematical-design-/basic-functions.html

Last edited by dirbax (2009-03-09 11:15:07)

Offline

Board footer

Powered by FluxBB