How does one create C++ code that prints the running product of every third number.
I don't know if I understand it or not.
How does one create C++ code that prints the running product of every third number.
I don't know if I understand it or not.
Don't know what you are running this on (32-bit architecture, 64-bit?) but you might be overflowing an int. Your answer is going to get very large.
The variable sum
is overflowing. Even making it an unsigned long long
will only handle up to 136. Using double
for sum
will get you close -- 481 -- but no data type available in C++ will handle all the way up to 500.
If you absolutely must go to 500, you're going to need to use a class that can support large integer values. You don't say what platform and compiler you're using, but take a look at something like the BigInteger class.
unsigned long product(100);
for (unsigned long i(103); i<=500; i+=3)
{
product *= i;
std::cout << product << std::endl;
}
So this iterates in steps of 3 every number from 100 to 500 and computes the product then outputs that.
Though as RobP says this will likely overflow, hence my using unsigned long's in the code above. To learn about detecting that overflow see this:
I am not sure if this can be accepted, but this prints a product (after all the format was not specified, right?).
unsigned long long product = 1;
std::vector<int> fct;
for (int i = 100; i <= 500; i = i +3)
{
if ( (i%2) == 0) {
int r = i / 2;
fct.push_back( r);
std::cout << "product:" << product << " * 2";
std::vector<int>::iterator it = fct.begin();
while ( it != fct.end()) {
std::cout << " * " << *it++;
}
std::cout << std::endl;
} else {
product = product * i;
std::cout << "product:" << product << " * 2";
std::vector<int>::iterator it = fct.begin();
while ( it != fct.end()) {
std::cout << " * " << *it++;
}
std::cout << std::endl;
}
}
Output:
product:1 * 2 * 50
product:103 * 2 * 50
product:103 * 2 * 50 * 53
product:11227 * 2 * 50 * 53
product:11227 * 2 * 50 * 53 * 56
product:1291105 * 2 * 50 * 53 * 56
product:1291105 * 2 * 50 * 53 * 56 * 59
(...)
product:2557801186831291169 * 2 * 50 * 53 * 56 * 59 * 62 * 65 * 68 * 71 * 74 * 77 * 80
product:2557801186831291169 * 2 * 50 * 53 * 56 * 59 * 62 * 65 * 68 * 71 * 74 * 77 * 80 * 83
(...)
product:9086141910361656411 * 2 * 50 * 53 * 56 * 59 * 62 * 65 * 68 * 71 * 74 * 77 * 80 * 83 * 86 * 89 * 92 * 95 * 98 * 101 * 104 * 107 * 110 * 113 * 116 * 119 * 122 * 125 * 128 * 131 * 134 * 137 * 140 * 143 * 146 * 149 * 152 * 155 * 158 * 161 * 164 * 167 * 170 * 173 * 176 * 179 * 182 * 185 * 188 * 191 * 194 * 197 * 200 * 203 * 206 * 209 * 212 * 215 * 218 * 221 * 224 * 227 * 230 * 233 * 236 * 239 * 242 * 245
product:9086141910361656411 * 2 * 50 * 53 * 56 * 59 * 62 * 65 * 68 * 71 * 74 * 77 * 80 * 83 * 86 * 89 * 92 * 95 * 98 * 101 * 104 * 107 * 110 * 113 * 116 * 119 * 122 * 125 * 128 * 131 * 134 * 137 * 140 * 143 * 146 * 149 * 152 * 155 * 158 * 161 * 164 * 167 * 170 * 173 * 176 * 179 * 182 * 185 * 188 * 191 * 194 * 197 * 200 * 203 * 206 * 209 * 212 * 215 * 218 * 221 * 224 * 227 * 230 * 233 * 236 * 239 * 242 * 245 * 248
product:14532515211626403169 * 2 * 50 * 53 * 56 * 59 * 62 * 65 * 68 * 71 * 74 * 77 * 80 * 83 * 86 * 89 * 92 * 95 * 98 * 101 * 104 * 107 * 110 * 113 * 116 * 119 * 122 * 125 * 128 * 131 * 134 * 137 * 140 * 143 * 146 * 149 * 152 * 155 * 158 * 161 * 164 * 167 * 170 * 173 * 176 * 179 * 182 * 185 * 188 * 191 * 194 * 197 * 200 * 203 * 206 * 209 * 212 * 215 * 218 * 221 * 224 * 227 * 230 * 233 * 236 * 239 * 242 * 245 * 248
The largest number you con go using integer data type is, unsigned long long int b = 0XFFFFFFFFFFFFFFFF; (18446744073709551615)
In your case the result is even bigger than this number.
That is the twist in this problem. You might have to find a way to handling large numbers. I recommend finding already existing API or algorithm.
I have written a program that does this... I did not test this for edge cases and there can be a better way of doing this.
For now,
#include <iostream>
#include <deque>
using namespace std;
void print_num(deque<int> &num) {
for(int i=0;i < num.size();i++) {
cout<<num[i];
}
cout<<endl;
}
deque<int> sum(deque<int> &oppA, deque<int> &oppB) {
if (oppA.size() == 0) return oppB;
if (oppB.size() == 0) return oppA;
deque<int> result;
unsigned int carry = 0;
deque<int>::reverse_iterator r_oppA = oppA.rbegin();
deque<int>::reverse_iterator r_oppB = oppB.rbegin();
while ((r_oppA != oppA.rend()) && (r_oppB != oppB.rend())) {
int tmp = *r_oppA + *r_oppB + carry;
result.push_front(tmp % 10);
carry = tmp / 10;
r_oppB++;
r_oppA++;
}
while (r_oppA != oppA.rend()) {
int tmp = *r_oppA + carry;
result.push_front(tmp % 10);
carry = tmp / 10;
r_oppA++;
}
while (r_oppB != oppB.rend()) {
int tmp = *r_oppB + carry;
result.push_front(tmp % 10);
carry = tmp / 10;
r_oppB++;
}
return result;
}
deque<int> multiply(deque<int>& multiplicand, deque<int>& multiplier) {
unsigned int carry = 0;
deque<int> result;
int deci_cnt = 0;
deque<int>::reverse_iterator r_multiplier = multiplier.rbegin();
deque<int> tmp_result;
while (r_multiplier != multiplier.rend()) {
for (int i=0; i<deci_cnt ;i++) {
tmp_result.push_front(0);
}
deque<int>::reverse_iterator r_multiplicand = multiplicand.rbegin();
while (r_multiplicand != multiplicand.rend()) {
int tmp = (*r_multiplicand) * (*r_multiplier) + carry;
tmp_result.push_front(tmp % 10);
carry = tmp / 10;
r_multiplicand++;
}
if (carry != 0) {
tmp_result.push_front(carry);
carry = 0;
}
result = sum(result, tmp_result);
deci_cnt++;
tmp_result.clear();
r_multiplier++;
}
return result;
}
deque<int> int_to_deque(unsigned long num) {
deque<int> result;
if (num == 0) {
result.push_front(0);
}
while (num > 0) {
result.push_front(num % 10);
num = num / 10;
}
return result;
}
int main() {
deque<int> num1 = int_to_deque(18446744073709551615ULL);
deque<int> num2 = int_to_deque(18446744073709551615ULL);
deque<int> result = multiply(num1, num2);
print_num(result);
return 0;
}
Output: 340282366920928463426481119284349108225