Challange: BASIC002
#1
Posted 07 March 2010 - 04:49 PM
eg.
my_function( my_function ( 5 ) ) = -5
You can use any language you like. Feel free to post any partial solutions you come up with also.
#2
Posted 07 March 2010 - 05:40 PM
Here my C++ solution.Write a function f which takes an int as an argument and where f(f(x)) = -x
eg.
my_function( my_function ( 5 ) ) = -5
You can use any language you like. Feel free to post any partial solutions you come up with also.
Excuse me, but I've solved my problem and I've posted the correct solution in the same post to take clear the topic!!
#include <iostream>
using namespace std;
int opposite ( int num ) ;
int main()
{
int n;
cout << "Insert a number " << endl;
cin >> n;
cout << "Opposite number = " << opposite ( n ) << endl;
return 0;
}
int opposite ( int num )
{
num =~ num;
return num+1;
}
Edited by AlexZ, 07 March 2010 - 05:46 PM.
#3
Posted 07 March 2010 - 06:05 PM
Here my C++ solution.
Write a function f which takes an int as an argument and where f(f(x)) = -x
eg.
my_function( my_function ( 5 ) ) = -5
You can use any language you like. Feel free to post any partial solutions you come up with also.
Excuse me, but I've solved my problem and I've posted the correct solution in the same post to take clear the topic!!#include <iostream> using namespace std; int opposite ( int num ) ; int main() { int n; cout << "Insert a number " << endl; cin >> n; cout << "Opposite number = " << opposite ( n ) << endl; return 0; } int opposite ( int num ) { num =~ num; return num+1; }
I'm afraid that is not correct. You are only applying opposite once, the challenge is to write a function which you apply twice to get the negative of the original argument:
opposite ( opposite ( x ) ) = -x
#4
Posted 08 March 2010 - 08:20 AM
Oh yes you're right!!Sorry...I'll try to do another function, but can you explain me better your exercise?
Here my C++ solution.
Write a function f which takes an int as an argument and where f(f(x)) = -x
eg.
my_function( my_function ( 5 ) ) = -5
You can use any language you like. Feel free to post any partial solutions you come up with also.
Excuse me, but I've solved my problem and I've posted the correct solution in the same post to take clear the topic!!#include <iostream> using namespace std; int opposite ( int num ) ; int main() { int n; cout << "Insert a number " << endl; cin >> n; cout << "Opposite number = " << opposite ( n ) << endl; return 0; } int opposite ( int num ) { num =~ num; return num+1; }
I'm afraid that is not correct. You are only applying opposite once, the challenge is to write a function which you apply twice to get the negative of the original argument:
opposite ( opposite ( x ) ) = -x
EDIT:
But this code works fine just like you've suggested to me
#include <iostream>
using namespace std;
int opposite ( int num ) ;
int main()
{
int n;
cout << "Insert a number " << endl;
cin >> n;
cout << "Opposite number = " << opposite ( opposite(n) ) << endl;
return 0;
}
int opposite ( int num )
{
if ( num > 0 )
{
num =~ num;
return num+1;
}
else { return num; }
}
Edited by AlexZ, 08 March 2010 - 08:31 AM.
#5
Posted 10 March 2010 - 11:15 AM
but it doesn't... :-(
edit: here's a cheaty, non-math way to do it... also python
Edited by JBu92, 17 September 2010 - 08:46 AM.
#6
Posted 11 April 2010 - 05:38 PM
#7
Posted 12 September 2010 - 04:00 PM
counterexample: -1Here is my solution in C.
Spoiler
#8
Posted 22 October 2010 - 02:03 PM
#9
Posted 19 December 2010 - 06:18 PM
BinRev is hosted by the great people at Lunarpages!













