Implementing Various Calculations ith only + and - in c++

In summary, the guy is trying to implement various operations using only + and -. He has been experimenting with the for loop but can't seem to make it work. He is also struggling with the square root and cube functions. He has also been asked about implementing the inverse factorial and prime number program. He is still thinking of it and would like some tips on how to do it.
  • #1
ermines
45
0
Guys, I'm starting to learn c++ and wanted to know hot to implement various operations using only + and -.

I'm trying to implement *, /, %, powers, and square root.

I've been experimenting with the for loop but can't seem to make it work. Any tips on how to do these?

Thanks :D
 
Physics news on Phys.org
  • #2
hmmm I've just finished the codes for *, /, %, and cube but they're all in int forms. is there a way to do this in float or double?

right now, I'm having a hard time implementing the cube, raised to any exponent and the square root.
 
  • #3
Using only addition and subtraction? Interesting problem. I'll think it over but I can't promise anything.
 
  • #4
ermines said:
hmmm I've just finished the codes for *, /, %, and cube but they're all in int forms. is there a way to do this in float or double?

right now, I'm having a hard time implementing the cube, raised to any exponent and the square root.
Just regular adds and subtracts, or bit level also? Like break numbers up into individual bits.
 
  • #5
I don't see any good way to do floating point arithemetic with only adding and subtracting. Square roots are going to be very difficult also since the square root of an integer is not, in general, an integer. Would the "integer part" (i.e. the largest integer less than or equal to the square root) be sufficient?

For powers such as Cube, here's what I would do

int Multiply(int x,int y)
{//returns x times y
int z= 0;
for (int i=0;i<x;i++)
z= z+ y;
return z;
}

int Square(int x)
{//returns x squared
return Multiply(x,x);
}

int Cube(int x)
{ //returns x cubed
int z= Square(x);
return Multiply(x,z);
}

int Power(int x, int n)
{ //returns x to the n power
int z= 1;
for (int i=0;i< n;i++)
z= Multiply(x,z);
return z;
}
 
  • #6
ermines said:
I'm trying to implement *, /, %, powers, and square root.

For what type of numbers? int,float,double,...

Do you also have to handle sign,overflow,divide by zero,...
I had this for a homework assignment once as well. It's all the special cases that make it hard.
 
  • #7
hmmm, thanks for the tips guys. I've finished everything, from * to exponentials, although they're in int form but handles positive and negative numbers.

My only problem now is the square root function of type int, not float. So how do i do it?
 
  • #8
yes! finally finished the code for square root thanks to the help of a ertain someone.

int sqRoot (int input)
{
int b=0;

while ((b*b)<=input)
{
b+=1;
}
b-=1;

return b;
}

now, if only i can find a way to implement some codes to float. any ideas, guys?
 
  • #9
How about
double sqRoot(double input)
{
double b=0;
while((b*b)<=input)
{
b=b+.0001;
}
return b;
}

Because you are looking at an approximate value, it won't make a big difference if you subtract by .0001 after the loop
 
  • #10
tsk, tsk, you used multiplication. :smile: What about calling your multiplication routine instead?
 
  • #11
ooooppppppsssss, you're right! i thought i had already edited it out. well, here's the revised edition. :D

int sqRoot (int input)
{
int b=0;

while (square (b)<=input)
{
b+=1;
}
b-=1;

return b;
}

umm now guys, I'm faced with a new problem. how do i implement the inverse factorial program and also a program that produces all non-prime numbers from 1-100?. of course, * and / are already allowed. I'm still thinking of it and have a rough idea of how to do it like probrably the same with the division code...Still, i need some tips and help on how to carry this out. :D
thanks again. :D
 
  • #12
i've already gotten the factorial code (below) but how do i do the inverse? do i just divide the return function to one? oh, wait! is inverse factorial first getting the factorial of a # then dividing it by one or taking the inverse of every factor?

int factorial (int x)

{
int base=1;
for (int i=1; i<=x; i++)
{
base*=i;
}
return base;
}
 
  • #13
What do you mean by "the inverse factorial"? Exactly what does the problem you were given say? I suspect you mean not the reciprocal but the function that, given n!, returns n. A problem with such a "function" is that it is not defined for most numbers.
 
  • #14
ohh yes, you are right! how could i have misunderstood what the teacher meant by inverse factorial. so if the input is 24, i should get 4.

here's my NON-WORKING code. how do i fix it. I am a beginner, remember. Basically i wan the input to be continuously divided until it reaches 1 and the i count the iterations to give me the base of the factorial.

int inv (int input)

{
int a=0;
while (input>=1)
{
a++;
input=input/(a+1);
}
a--;
return a;
}
 
  • #15
my code is pretty clunky, but here's something I was playing around with:

int main()
{
int number = 24; //the input
int div = 1;
int count = 0;

while(number > 1)
{
number = number/div;
div ++;
count ++;
}

cout << count;

return 0;
}
 
Last edited:
  • #16
i already got it, i think. here's my revised code. pls check if there is more needed to be added.

int inv (int n)

{
int a=1;
if (n>=0)
{
while (n>=1)
{
a++;
n=n/a;
}
a--;
}
return a;
}

now all i need is the code for listing all non-prime numbers from 1-100 including 100. any tips?
 
  • #17
ermines said:
i already got it, i think. here's my revised code. pls check if there is more needed to be added.

int inv (int n)

{
int a=1;
if (n>=0)
{
while (n>=1)
{
a++;
n=n/a;
}
a--;
}
return a;
}

now all i need is the code for listing all non-prime numbers from 1-100 including 100. any tips?

This may not be the most efficient method, but try this:

Code:
for (int i = 1; i <= 100; i++) {
    for (int j = 2; j < i; j++) {
        if (Modulus(i, j) == 0) {
            cout << i << endl;
            break;
        }
    }
}
 
  • #18
umm what is the modulus for? isn't it undeclared?
 
  • #19
Whoops I meant this:

Code:
for (int i = 1; i <= 100; i++) {
    for (int j = 2; j < i; j++) {
        if (i % j == 0) {
            cout << i << endl;
            break;
        }
    }
}

Basically once it finds any factor other than 1 and the number itself, then it is not a prime number and therefore stops the inner loop and moves on to the next number.
 
  • #20
Also, it is apparently obvious that 1 is not prime nor composite so that is never listed. And 2 is obviously prime since there are no numbers between 1 and 2 to check for factors.
 
  • #21
ohhh i see. sorry for not noticing it quickly. modulus is the code for finding the remainder. wow, this is such a cool code! i didn't even thought of using such a method. thanks! :D
 
  • #22
umm guys, i just have this other question. How do i implement the "Press enter key to continue"? I researched that the ascii value of enter key is 13. so do i just do an if cin=='13' for the cin if it is equal to 13? But what would be its difference to the number 13?
 

1. How can I perform multiplication and division using only addition and subtraction in C++?

To perform multiplication in C++ using only addition and subtraction, you can use a loop to repeatedly add a number to itself a certain number of times. For example, to multiply 4 and 5, you can add 4 to itself 5 times (4+4+4+4+4=20). To perform division, you can use a similar approach by repeatedly subtracting a number from itself until you reach the desired quotient.

2. Is it possible to create a program that can handle complex calculations using only addition and subtraction?

Yes, it is possible to create a program that can handle complex calculations using only addition and subtraction. This can be achieved by breaking down the complex calculations into smaller, simpler steps that can be performed with just addition and subtraction. Additionally, using variables and control structures such as loops and conditional statements can help in implementing more complex calculations.

3. How do I handle negative numbers when performing calculations with only addition and subtraction?

When dealing with negative numbers in calculations using only addition and subtraction, you can use the concept of absolute value. Absolute value is the distance between a number and zero on a number line. This means that the absolute value of a negative number is the positive version of the same number. So, when subtracting a negative number, you can convert it to its absolute value and then perform the subtraction as usual.

4. Can I implement functions and mathematical operations like square root and exponentiation using only addition and subtraction?

Yes, you can implement functions and mathematical operations like square root and exponentiation using only addition and subtraction. For example, to find the square root of a number, you can use a loop and repeatedly subtract smaller numbers from the given number until you reach the closest possible value. Similarly, exponentiation can be achieved by repeatedly multiplying a number by itself a certain number of times.

5. What are the advantages and limitations of using only addition and subtraction in calculations?

One advantage of using only addition and subtraction in calculations is that it can help in understanding the fundamentals of arithmetic operations and can be a good exercise for problem-solving skills. However, it can become more complex and time-consuming when dealing with larger numbers or more complex calculations. Additionally, some mathematical operations, such as division, may not be as accurate compared to using dedicated operators and functions.

Similar threads

  • Introductory Physics Homework Help
Replies
25
Views
256
  • Introductory Physics Homework Help
Replies
5
Views
359
  • Introductory Physics Homework Help
Replies
3
Views
884
  • Introductory Physics Homework Help
Replies
5
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
Replies
1
Views
1K
  • Introductory Physics Homework Help
Replies
4
Views
1K
  • Introductory Physics Homework Help
Replies
11
Views
613
  • Introductory Physics Homework Help
Replies
2
Views
2K
  • Introductory Physics Homework Help
Replies
6
Views
4K
Back
Top