# Math/Programming Puzzle: Use of functions

Printable View

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-03-2003
KingZoolerius66
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.
• 10-03-2003
KingZoolerius66
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;   // Do your *stuff* here   // 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).
• 10-03-2003
procyon4476
Am I the only one who thinks he is trying to trick us into doing his homework?
• 10-03-2003
KingZoolerius66
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.
• 10-03-2003
joshdick
Quote:

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.
• 10-03-2003
jlou
Interesting puzzle. I got an answer for your first one.

By the way, your code needs a using directive for cin.
• 10-03-2003
KingZoolerius66
no it doesn't.

I never used cin so I don't need an using directive.
• 10-03-2003
XSquared
You're right. cin.get( ) doesn't use cin at all. :rolleyes: :rolleyes:
• 10-03-2003
KingZoolerius66
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-04-2003
Karshikinpa
frist: tempNum = a(b(cNum))+cNum;

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

i'll come up with one in a minute...
• 10-04-2003
Karshikinpa
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
• 10-04-2003
Karshikinpa
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.
• 10-04-2003
KingZoolerius66
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; }```
• 10-04-2003
KingZoolerius66
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; }```
• 10-04-2003
Karshikinpa
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) )))
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last