Karabee Batta
Karabee Batta

Reputation: 1

how can i optimise this code so that it takes lesser run time?

The original question is:

A man has a rather old car being worth $2000. He saw a secondhand car being worth $8000. He wants to keep his old car until he can buy the secondhand one.

He thinks he can save $1000 each month but the prices of his old car and of the new one decrease of 1.5 percent per month. Furthermore the percent of loss increases by a fixed 0.5 percent at the end of every two months.

Can you help him? Our man finds it difficult to make all these calculations.

How many months will it take him to save up enough money to buy the car he wants, and how much money will he have left over?

I have implemented this so far how can I improve this code.

int main (){
    double percent=0.985; // the value by which the price of an item //reduces
    int startPriceOld=2000; //price of an item i want to sell
    int startPriceNew=8000;// price of an item i want to buy
    int savingperMonth=1000; // my monthly savings  
    int init=0;
    int mon=0; //no of months
    int saving=0;
    int diff=(saving*mon)-((pow(percent,mon))*(startPriceNew-startPriceOld));
      /*checking the condition when the money I have by selling my old item in addition to my savings will be greater than the money I need to buy my new item*/
      while(1)
      {
          mon++;
          startPriceOld=percent*startPriceOld;
          startPriceNew=percent*startPriceNew;
          saving=init+mon*savingperMonth;
          if(diff>0)
              {break;}
      }
          int arr[2];
          arr[0]=mon;
          arr[1]=diff;
          printf("%d",mon);
}

Upvotes: 0

Views: 89

Answers (3)

LucaLumetti
LucaLumetti

Reputation: 340

You can solve this with just math:

Price of the new car at each month:
(new_car_cost)*(1-(0.015+0.005*floor(month/2)))^month

Price of the old car at each month:
(old_car_cost)*(1-(0.015+0.005*floor(month/2))^month

Your salary:
1000*month

At the nth-month you will have:
new_car_cost = old_car_cost+salary

round up the month and you have solved the problem

You can also semplify the equation.

Upvotes: 1

Gromov Anton
Gromov Anton

Reputation: 150

Output in main loop is for understanding what's happening there :)

#include <stdio.h>
#include <stdlib.h>

int main ( )
{
    /* will store it in cents */

    unsigned int money_now = 0; 
    unsigned int salary = 100000;
    unsigned int money_left;

    unsigned int old_car_cost = 200000;
    unsigned int new_car_cost = 800000;

    float decr_percent = 0.015;
    float decr_percent_diff = 0.005;

    size_t months = 0;

    while ( 1 )
    {
        months++; /* Month has passed */

        money_now += salary; /* got salary*/
        old_car_cost -= old_car_cost * decr_percent; /* new cost of old car */
        new_car_cost -= new_car_cost * decr_percent; /* new cost of new car */

        /* 
            meanings:
            b - bugdet;
            p - percent;
            oc - old car cost;
            nc - new car cost.
        */
        printf("b:$%u;\tp:%.1f;\toc:$%u.%u;\tnc:$%u.%u\n", 
            money_now / 100, decr_percent * 100, old_car_cost / 100, old_car_cost % 100, new_car_cost / 100, new_car_cost % 100 );

        /* if ( ours money + old car's cost ) is enough for new car  */
        if ( new_car_cost <= ( money_now + old_car_cost) ) break;

        /*if it's end of every second month */
        if ( 0 == (months % 2) ) decr_percent += decr_percent_diff;
    }

    puts(""); /* newline */
    printf("It has been %lu months.\n", months );
    money_left = ( money_now + old_car_cost ) - new_car_cost;
    printf("Money left : $%u.%u\n", money_left / 100, money_left % 100);

    return 0;
}

Upvotes: 0

Aniruddh Agarwal
Aniruddh Agarwal

Reputation: 917

Tried solving your issue and reached to this solution:

#include <stdio.h>

    int main (){
        float priceOld=2000; //price of an item i want to sell
        float priceNew=8000;// price of an item i want to buy
        int savingperMonth=1000; // my monthly savings
        int finalMonths = 0;
        float finalLeft = 0;
        int mon=0; //no of months
        for (mon = 1; mon<9;mon++){
            priceOld = priceOld - ((priceOld*15)/100);
            priceNew = priceNew - ((priceNew*15)/100);
            if( mon % 2 == 0 ){
                priceOld = priceOld - ((priceOld*5)/100);
                priceNew = priceNew - ((priceNew*5)/100);
            }
            if((priceOld + (savingperMonth*mon)) >= priceNew){
                finalMonths = mon;
                finalLeft = (priceOld + (savingperMonth*mon)) - priceNew;
                printf("Number of months: %d\n", finalMonths);
                printf("Final amount left: %f$", finalLeft);
                break;
            }
        }
    return 0;
    }

I think This will work fine for you.!

Upvotes: 0

Related Questions