Jump to content


Photo

Challange: BASIC002


  • Please log in to reply
8 replies to this topic

#1 rainwater_stillicide

rainwater_stillicide

    SUP3R 31337 P1MP

  • Agents of the Revolution
  • 282 posts
  • Location:Scotland

Posted 07 March 2010 - 04:49 PM

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.

#2 AlexZ

AlexZ

    elite

  • Members
  • 114 posts
  • Country:
  • Gender:Male

Posted 07 March 2010 - 05:40 PM

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.

Here my C++ solution.
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 rainwater_stillicide

rainwater_stillicide

    SUP3R 31337 P1MP

  • Agents of the Revolution
  • 282 posts
  • Location:Scotland

Posted 07 March 2010 - 06:05 PM


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.

Here my C++ solution.
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 AlexZ

AlexZ

    elite

  • Members
  • 114 posts
  • Country:
  • Gender:Male

Posted 08 March 2010 - 08:20 AM



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.

Here my C++ solution.
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

Oh yes you're right!!Sorry...I'll try to do another function, but can you explain me better your exercise?
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 JBu92

JBu92

    HACK THE PLANET!

  • Members
  • 58 posts
  • Country:
  • Gender:Male

Posted 10 March 2010 - 11:15 AM

well if python worked with imaginary numbers:
Spoiler

but it doesn't... :-(
edit: here's a cheaty, non-math way to do it... also python
Spoiler

Edited by JBu92, 17 September 2010 - 08:46 AM.


#6 systmkor

systmkor

    n00bie

  • Members
  • 10 posts

Posted 11 April 2010 - 05:38 PM

Here is my solution in C.

Spoiler


#7 JBu92

JBu92

    HACK THE PLANET!

  • Members
  • 58 posts
  • Country:
  • Gender:Male

Posted 12 September 2010 - 04:00 PM

Here is my solution in C.

Spoiler

counterexample: -1

#8 codered22

codered22

    Will I break 10 posts?

  • Members
  • 5 posts
  • Gender:Male

Posted 22 October 2010 - 02:03 PM

Here's mine. :)

Spoiler


#9 heisenbug

heisenbug

    Gibson Hacker

  • Members
  • 79 posts
  • Country:
  • Gender:Male

Posted 19 December 2010 - 06:18 PM

Spoiler





BinRev is hosted by the great people at Lunarpages!