primtiive calculator in c 2b 2b

Solutions on MaxInterview for primtiive calculator in c 2b 2b by the best coders in the world

showing results for - "primtiive calculator in c 2b 2b"
Coco
24 Oct 2020
1#include <iostream>
2#include <vector>
3#include <algorithm>
4#include <stdio.h>
5#include <stdlib.h>
6
7long long f(long long n, vector <long long> arr)
8{ 
9    arr[1]=0;
10
11    arr.push_back(n);
12    long long ans=0, ret=0;
13
14    if (n==1)
15    {
16        return (0);
17    }
18    ans= f(n-1, arr) + 1;
19
20
21    if (n%2==0)
22    {
23        ret= f(n/2, arr) + 1;
24        if (ret<ans)
25        {
26            ans=ret;
27            std::cout<<ans<<'\n';
28        }
29    }
30    if (n%3==0)
31    { 
32        ret= f(n/3, arr) + 1;
33        if (ret<ans)
34        {
35            ans=ret;
36            std::cout<<ans<<'\n';
37        }
38    }
39    arr[n]=ans;
40
41    return arr[n];    
42}
43
44int main() {
45
46    long long n;
47    std::cin >> n;
48    std::vector<long long> arr;
49    std::cout<<f(n, arr);
50
51    return 0;
52}