3

I had an interview today. This question was to optimize the code below. if we will see the code below after for loop there are four steps of "if-else" follows. So, interviewer asked me optimize it to 3 if-else line. I have tried a lot. But could not able to find the solution. Even he told me if you know scripting language then, you can use them also. Please help me in optimizing the same.

int main()
{
    int i = 1;
    for(i; i <= 100; i++)
    {
        if((i % 3 == 0 && i % 5 == 0))
        {cout << "PR\n";}
        else if(i % 3 == 0)
        {cout << "P\n";}
        else if(i % 5 == 0)
        {cout << "R\n";}
        else
        {cout << i <<"\n";}
    }
system("pause");
return 0;
}
FreelanceConsultant
  • 13,167
  • 27
  • 115
  • 225
Rasmi Ranjan Nayak
  • 11,510
  • 29
  • 82
  • 122
  • Shouldn't `"P"` and `"R"` be `"F"` and `"B"`, respectively? – 6502 Sep 04 '13 at 16:11
  • @6502: I didn't get your question – Rasmi Ranjan Nayak Sep 04 '13 at 16:13
  • 1
    This is the [FizzBuzz](http://rosettacode.org/wiki/FizzBuzz) problem (@6502's "F" and "B" reference). – Ken White Sep 04 '13 at 16:31
  • Though the assignment is pretty trivial, I daresay that rhe really correct answer is "WTF, outputting to cout takes 10,000 longer than an extra if, that is a useless optimization". In other words, this is probably a catch question. – Damon Sep 04 '13 at 16:34

5 Answers5

4

This is a well known question... the "FizzBuzz".

You can even solve it without any explicit IFs

const char *messages[] = {"%i\n", "P\n", "R\n", "PR\n"};

for (i=1; i<=100; i++) {
    printf(messages[((i % 3)==0) + 2*((i % 5)==0))], i);
}
6502
  • 112,025
  • 15
  • 165
  • 265
3

Here's one way, in Python:

for i in range(1, 101):
    s = ''
    if i % 3 == 0:
        s += 'P'
    if i % 5 == 0:
        s += 'R'
    if i % 3 != 0 and i % 5 != 0:
        s = i
    print(s)

Equivalently: using a flag, as shown in your own answer:

for i in range(1, 101):
    s, flag = '', False
    if i % 3 == 0:
        flag = True
        s += 'P'
    if i % 5 == 0:
        flag = True
        s += 'R'
    if not flag:
        s = i
    print(s)

Just for fun, a Python version of @6502's answer:

messages = ['{}', 'P', 'R', 'PR']
for i in range(1, 101):
    print(messages[(i%3 == 0) + 2*(i%5 == 0)].format(i))

And finally, my personal favorite (because it's the shortest) - using the Greatest Common Divisor function and a lookup table:

from fractions import gcd
messages = {3:'P', 5:'R', 15:'PR'}
for i in range(1, 101):
    print(messages.get(gcd(i, 15), i))
Óscar López
  • 232,561
  • 37
  • 312
  • 386
2

I found a solution. Please let me know whether it is good or not?

int main()
{
    int i = 1;int stat=0;
    for(i; i <= 100; i++)
    {
        stat=0;
        if(i%3 == 0){stat++; cout << "P";}
        if(i%5 == 0){stat++; cout << "R";}
        if(stat == 0)cout << i;
        cout << "\n";
    }
system("pause");
return 0;
}
Rasmi Ranjan Nayak
  • 11,510
  • 29
  • 82
  • 122
  • This answer is correct, but the solution to this problem can be written in a simpler way - there's no need to use `if-else if-else` instructions! see my answer for a couple of options – Óscar López Sep 04 '13 at 18:21
1

I really like 6502's answer, but here is a simple solution without extra variables:

for(i = 1; i <= 100; i++)
{
    if(i % 3 != 0 && i % 5 != 0)
    {
        printf("%d\n", i);
        continue;
    }

    if(i % 3 == 0)
        printf("P");

    if(i % 5 == 0)
        printf("R");

    printf("\n");
}
Dima
  • 568
  • 4
  • 7
0

this way only use 3 if

#include <iostream>
using namespace std;

int main()
{
 for (int i = 0; i <= 100; ++i)
 {
   bool fizz = (i % 3) == 0;
   bool buzz = (i % 5) == 0;
   if (fizz)
    cout << "Fizz";
   if (buzz)
    cout << "Buzz";
    if (!fizz && !buzz)
     cout << i;
    cout << endl;
  }
  return 0;
}
Simeng Yue
  • 11
  • 3