# Thread: Math/Programming Puzzle: Use of functions

1. ## Math/Programming Puzzle: Use of functions

This little *puzzle* I made are for those C/C++ programmers who are good with math and such. In the following source code, I will make a bunch of functions that will take a constant numeric variable and perform mathematical operations with that constant variable. Now, the purpose of the puzzle is for you programmers to use those functions to reach a certain number. You could use the function as many times as you'd like.

Here is an example:
const variable= 9;
goal to reach= 63;

Code:
```#include <iostream>

using std::cout;

int a( int );
int b( int );

int main( int argc, char *argv[] )
{
const int cNum= 9;
int tempNum;

tempNum= a( cNum );
tempNum= b( cNum ) + a( tempNum );

cout << tempNum;

cin.get();
return 0;
}

int a( int Number )
{
return( Number * 2 );
}

int b( int Number )
{
return( Number * 3 );
}```
I hope you understand the concept. I will post the first *mission* next.

2. You cannot make a new function. You cannot use a pre-made function from your compiler's include folder. You could set up a new variable that will help you also. Here is the first *mission*.

const variable: 6;
goal: 27;

Code:
```#include <iostream>

using std::cout;

int a( int );
int b( int );

int main( int argc, char *argv[] )
{
const int cNum= 6;

// Yep.....

cin.get();
return 0;
}

int a( int Number )
{
return( Number * 3 );
}

int b( int Number )
{
return( Number / 2 );
}```
When you call my functions you have to use cNum as a parameter(or another variable that is somehow equal to cNum. ex: int c= cNum * 9; c= b( c ) + cNum; something like that).

3. Am I the only one who thinks he is trying to trick us into doing his homework?

4. Yeah, you're the only one. I don't have any computer science classes at my school, nor do I have anything related to this at my school.

This isn't homework. This is a fun little puzzle I came up with and just thought I'd post it at the cboard.

5. Originally posted by procyon4476
Am I the only one who thinks he is trying to trick us into doing his homework?
Yes, but I don't think this "challenge" is interesting enough to be worth it.

By the way, your code needs a using directive for cin.

7. no it doesn't.

I never used cin so I don't need an using directive.

8. You're right. cin.get( ) doesn't use cin at all.

9. Oops, I feel like an idiot. If you're able to solve that simple puzzle then construct your own puzzle for the next poster to solve and then so on.

Keep the ball rolling

10. frist: tempNum = a(b(cNum))+cNum;

second: tempNum = b(a(a(cNum)));

i'll come up with one in a minute...

11. using the functions from the second one:

Code:
```int a( int Number )
{
return( Number * 3 );
}

int b( int Number )
{
return( Number / 2 );
}```
source = 51

goal = 7

12. also, see what's the least number of function calls you can get them down too. for the one i posted, i got it down to 5 so far.

13. I got it down to 6 on my first try but then tried again and got it.

Code:
```int main( int argc, char *argv[] )
{
// goal: 7
const int cNum= 51;
int TempNum;

TempNum= a(a(cNum));
TempNum= b(b(b(b(b(b(TempNum))))));

cout << TempNum;

cin.get();
return 0;

}```

14. Heres one:
You may use any standard C++ functions from any header. You may use the utility function provided. The '=' character must appear exactly once (when you assign to fillme). You may declare extra POD-type variables.

Code:
```// Source: 4;
// Goal: 20;

// utility function:
template <class T, size_t N>
T* a(T(&b)[N])
{
return b+N;
}

int main()
{
const int num = 4;
int fillme;
// (insert code here)

cout << fillme;
cin.get();
return 0;
}```

15. for the one i posted, here is a way to do it with 6 function calls, i can't remember how i did it last night with 5 right now =/. I'll have to come up with it again.

b(b(b( (b(b(b(cNum)))+cNum) )))