question_slug
stringlengths 3
77
| title
stringlengths 1
183
| slug
stringlengths 12
45
| summary
stringlengths 1
160
⌀ | author
stringlengths 2
30
| certification
stringclasses 2
values | created_at
stringdate 2013-10-25 17:32:12
2025-04-12 09:38:24
| updated_at
stringdate 2013-10-25 17:32:12
2025-04-12 09:38:24
| hit_count
int64 0
10.6M
| has_video
bool 2
classes | content
stringlengths 4
576k
| upvotes
int64 0
11.5k
| downvotes
int64 0
358
| tags
stringlengths 2
193
| comments
int64 0
2.56k
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
minimum-deletions-to-make-string-k-special | C++ using memo and sort-counting | c-using-memo-and-sort-counting-by-aavii-iqq4 | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | aavii | NORMAL | 2024-03-28T17:38:58.459555+00:00 | 2024-03-28T17:38:58.459588+00:00 | 9 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n int dp[27][27];\n int solve(vector<int> &nums, int i, int j, int k)\n {\n if(i>=j) return 0;\n\n if(dp[i][j]!=-1) return dp[i][j];\n int a = nums[i] + solve(nums, i+1, j, k);\n int x = (nums[j]-nums[i])-k;\n int b = ( x >= 0 ? x : 0 )+ solve(nums, i, j-1, k);\n \n return dp[i][j] = min(a, b);\n }\n int minimumDeletions(string word, int k) {\n \n unordered_map<char,int> mp;\n for(auto i:word) mp[i]++;\n\n vector<int> nums;\n for(auto i:mp) nums.push_back(i.second);\n sort(nums.begin(), nums.end());\n memset(dp, -1, sizeof(dp));\n\n return solve(nums, 0, nums.size()-1, k);\n\n }\n};\n``` | 0 | 0 | ['C++'] | 0 |
minimum-deletions-to-make-string-k-special | [Java] Simple solution frequency array | java-simple-solution-frequency-array-by-3haxb | Upvote if you learn something new :)\n\nclass Solution {\n public int minimumDeletions(String word, int k) {\n int[] freq = new int[26];\n \n | Naresh_Choudhary | NORMAL | 2024-03-27T13:03:38.411667+00:00 | 2024-03-27T13:03:38.411710+00:00 | 16 | false | **Upvote if you learn something new :)**\n```\nclass Solution {\n public int minimumDeletions(String word, int k) {\n int[] freq = new int[26];\n \n for(char c : word.toCharArray()){\n freq[c-\'a\']++;\n }\n \n int min = Integer.MAX_VALUE;\n for(int i=0; i<26; i++){\n int del = 0;\n for(int j=0; j<26; j++){\n if(freq[j] < freq[i])\n del += freq[j];\n else if(freq[j] > freq[i] + k)\n del += (freq[j]-freq[i]-k);\n }\n \n min = Math.min(min, del);\n }\n \n \n return min;\n }\n}\n``` | 0 | 0 | ['Java'] | 0 |
minimum-deletions-to-make-string-k-special | easy to understand c++ | easy-to-understand-c-by-warril-x7jd | all steps according to hints\n\n# Code\n\nclass Solution {\npublic:\n int minimumDeletions(string word, int k) {\n\n unordered_map<char,int>m;\n\n | warril | NORMAL | 2024-03-27T10:11:27.842318+00:00 | 2024-03-27T10:11:27.842349+00:00 | 19 | false | all steps according to hints\n\n# Code\n```\nclass Solution {\npublic:\n int minimumDeletions(string word, int k) {\n\n unordered_map<char,int>m;\n\n for(auto &i:word)\n m[i]++;\n\n vector<int>freq;\n for(auto i:m)\n {freq.push_back(i.second);}\n\n sort(freq.begin(), freq.end(), greater<int>());\n\n int ans = INT_MAX; \n\n for (int i = 0; i < freq.size(); i++) {\n int num = freq[i]; \n int cnt = 0;\n\n for (int j = 0; j < freq.size(); j++) {\n if (freq[j] - num > k) {\n cnt += freq[j] - (num + k);\n } \n else if (num > freq[j]) {\n cnt += freq[j];\n }\n }\n ans = min(ans, cnt);\n if (cnt == 0) {\n break;\n }\n }\n return ans;\n }\n};\n``` | 0 | 0 | ['C++'] | 0 |
minimum-deletions-to-make-string-k-special | C++ solution | c-solution-by-bhanutechie-0tcf | Code\n\nclass Solution {\npublic:\n int minimumDeletions(string word, int k) {\n unordered_map<char, int> m;\n int n = word.size();\n fo | bhanutechie | NORMAL | 2024-03-26T08:31:59.919139+00:00 | 2024-03-26T08:31:59.919171+00:00 | 12 | false | # Code\n```\nclass Solution {\npublic:\n int minimumDeletions(string word, int k) {\n unordered_map<char, int> m;\n int n = word.size();\n for(int i = 0; i < n; i++) {\n m[word[i]]++;\n }\n vector<int> v;\n for(auto it : m) {\n v.push_back(it.second);\n }\n sort(v.begin(), v.end());\n int s = v.size();\n int ans = INT_MAX;\n int x = 0;\n for(int i = 0; i < s; i++) {\n int y = 0;\n for(int j = s - 1; j > i; j--) {\n if(v[j] - v[i] <= k) break;\n y += v[j] - v[i] - k;\n }\n y += x;\n ans = min(ans, y);\n x += v[i];\n }\n return ans;\n }\n};\n``` | 0 | 0 | ['C++'] | 0 |
minimum-deletions-to-make-string-k-special | simple and easy | simple-and-easy-by-hardik_3010-uzg9 | \n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(1)\n\n# Code\n\nclass Solution {\npublic:\n int minimumDeletions(string word, int k) {\n | hardik_3010 | NORMAL | 2024-03-25T14:27:45.090883+00:00 | 2024-03-25T14:27:45.090921+00:00 | 8 | false | \n# Complexity\n- Time complexity:\n**O(n)**\n\n- Space complexity:\n**O(1)**\n\n# Code\n```\nclass Solution {\npublic:\n int minimumDeletions(string word, int k) {\n vector<int> freq(26, 0);\n\n for(char &ch : word) {\n freq[ch-\'a\']++;\n }\n\n sort(begin(freq), end(freq));\n\n int result = INT_MAX;\n int deleted_till_now = 0;\n\n for(int i = 0; i < 26; i++) {\n\n int minFreq = freq[i];\n int temp = deleted_till_now; //temp taken to find deletion for j = 25 to j > i\n\n for(int j = 25; j > i; j--) {\n if(freq[j] - freq[i] <= k) \n break;\n \n temp += freq[j] - minFreq - k;\n }\n\n result = min(result, temp);\n deleted_till_now += minFreq;\n\n }\n\n return result;\n }\n};\n``` | 0 | 0 | ['Two Pointers', 'Sorting', 'C++'] | 0 |
minimum-deletions-to-make-string-k-special | scala solution | scala-solution-by-lyk4411-7a13 | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | lyk4411 | NORMAL | 2024-03-25T09:13:13.190770+00:00 | 2024-03-25T09:15:53.329720+00:00 | 10 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nobject Solution {\n def minimumDeletions(word: String, k: Int): Int = {\n val freq = word.groupBy(identity).view.mapValues(_.size).values\n if(freq.size == 1 || freq.min + k >= freq.max) return 0\n (freq.min + k to freq.max).foldLeft(Int.MaxValue)((acc, cur) =>{\n (freq.filter(_ < cur - k).sum + freq.filter(_ > cur).map(_ - cur).sum) min acc\n })\n }\n}\n``` | 0 | 0 | ['Scala'] | 0 |
minimum-deletions-to-make-string-k-special | Easy to understand code | easy-to-understand-code-by-ps02-xvoa | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | PS02 | NORMAL | 2024-03-25T08:50:57.376693+00:00 | 2024-03-25T08:50:57.376727+00:00 | 4 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(N)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(N)\n\n# Code\n```\nclass Solution:\n def minimumDeletions(self, word: str, k: int) -> int:\n word=list(word)\n count = Counter(word)\n ans=1000000\n for x in count.values():\n op=0\n for y in count.values():\n if y < x:\n op+=y\n if y-x>k:\n op+=y-x-k\n ans=min(op,ans)\n return ans\n\n\n\n \n``` | 0 | 0 | ['Python3'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | ✅ [Python/C++] prime factorization (explained) | pythonc-prime-factorization-explained-by-tnui | \u2705 IF YOU LIKE THIS SOLUTION, PLEASE UPVOTE.\n*\nThis solution employs prime factorization.\n\n\nComment. The idea of this solution relies on the fact that | stanislav-iablokov | NORMAL | 2022-12-18T04:01:36.525821+00:00 | 2022-12-18T07:45:31.254379+00:00 | 5,635 | false | **\u2705 IF YOU LIKE THIS SOLUTION, PLEASE UPVOTE.**\n****\nThis solution employs prime factorization.\n****\n\n**Comment.** The idea of this solution relies on the fact that a number is not less than the sum of its primes. Equality is attained for the prime numbers themselves and also for the number 4. Thus we can iteratively decompose `n` into primes and compute their sum until the sequence stabilizes.\n\n**Python #1.**\n```\nclass Solution:\n def smallestValue(self, n: int) -> int:\n \n def primes(n, s=0):\n for i in range(2,n+1):\n while n % i == 0:\n s += i\n n //= i\n return s\n \n while n != (n:=primes(n)): pass\n \n return n\n```\n\n**Python #2.** This can be compactified even further.\n```\nclass Solution:\n def smallestValue(self, n: int) -> int:\n\n m, s = n, 0\n for i in range(2,n+1):\n while m % i == 0:\n s += i\n m //= i\n\n return s if s == n else self.smallestValue(s)\n```\n\n**C++.** The solution above compressed into three lines of code.\n```\nclass Solution \n{\npublic:\n int smallestValue(int n, int s = 0) \n {\n for (int i = 2, m = n; i <= n; i++)\n for(; m % i == 0; s += i, m /= i);\n return s == n ? n : smallestValue(s);\n }\n};\n``` | 41 | 10 | [] | 9 |
smallest-value-after-replacing-with-sum-of-prime-factors | C++ || Simple Prime Factorization | c-simple-prime-factorization-by-mohakhar-4de3 | \nclass Solution {\npublic:\n long long getSumOfFactors(int n)\n {\n int divisor = 2;\n long long ans = 0;\n while(n > 1)\n {\ | mohakharjani | NORMAL | 2022-12-18T04:06:42.735926+00:00 | 2022-12-18T04:06:42.735963+00:00 | 3,423 | false | ```\nclass Solution {\npublic:\n long long getSumOfFactors(int n)\n {\n int divisor = 2;\n long long ans = 0;\n while(n > 1)\n {\n if (n % divisor == 0) \n {\n ans += divisor;\n n = n / divisor;\n }\n else divisor++;\n }\n return ans;\n }\n int smallestValue(int n) \n { \n while(true)\n {\n long long sumOfFactors = getSumOfFactors(n);\n if (n == sumOfFactors) break;\n n = sumOfFactors;\n }\n return n;\n }\n};\n``` | 36 | 5 | ['C', 'C++'] | 6 |
smallest-value-after-replacing-with-sum-of-prime-factors | simple java prime factorization | simple-java-prime-factorization-by-flyro-mko9 | \nclass Solution {\n public int smallestValue(int n) {\n int sum=n;\n while(true){\n int sum1=0;\n int c=2;\n | flyRoko123 | NORMAL | 2022-12-18T04:46:31.393756+00:00 | 2022-12-18T04:46:31.393795+00:00 | 2,031 | false | ```\nclass Solution {\n public int smallestValue(int n) {\n int sum=n;\n while(true){\n int sum1=0;\n int c=2;\n //for prime factors\n while(n>1){\n \n if(n%c==0){\n sum1+=c;\n n=n/c;\n }\n else c++;\n }\n n=sum1;\n // no more less sum can encounter\n if(sum==sum1)break;\n else sum=sum1;\n }\n return sum;\n }\n}\n``` | 28 | 0 | ['Math', 'Greedy', 'Java'] | 5 |
smallest-value-after-replacing-with-sum-of-prime-factors | Python 3 || 10 lines, recursion, w/ brief explanation || T/S: 42% / 90% | python-3-10-lines-recursion-w-brief-expl-tbkt | \nclass Solution:\n def smallestValue(self, n):\n\n prev, ans = n, 0\n\n while not n%2: # 2 is the unique even prime\n | Spaulding_ | NORMAL | 2022-12-18T21:01:18.699217+00:00 | 2024-06-16T21:02:45.454524+00:00 | 1,153 | false | ```\nclass Solution:\n def smallestValue(self, n):\n\n prev, ans = n, 0\n\n while not n%2: # 2 is the unique even prime\n ans += 2\n n//= 2\n\n for i in range(3,n+1,2): # <\u2013\u2013 prune even divisors...\n while not n%i:\n ans += i\n n//= i\n\n return self.smallestValue(ans) if ans != prev else ans\n```\n[https://leetcode.com/problems/smallest-value-after-replacing-with-sum-of-prime-factors/submissions/1290506391/](https://leetcode.com/problems/smallest-value-after-replacing-with-sum-of-prime-factors/submissions/1290506391/)\n\n\nI could be wrong, but I think that time complexity is *O*(sqrt *N* * log *N*) and space complexity is *O*(log *N*), in which *N* ~ `n`.\n | 14 | 0 | ['Python', 'Python3'] | 1 |
smallest-value-after-replacing-with-sum-of-prime-factors | Prime Factorization | prime-factorization-by-votrubac-hqni | We prepare the list of primes to cover up to sqrt(10,000). You can either precompute them, or just use a static array.\n\n> This is an important optimization. W | votrubac | NORMAL | 2022-12-18T04:01:23.407690+00:00 | 2022-12-21T00:05:45.138845+00:00 | 2,314 | false | We prepare the list of primes to cover up to `sqrt(10,000)`. You can either precompute them, or just use a static array.\n\n> This is an important optimization. We check up to 65 primes for `n`, and the complexity is \u03C0(sqrt(n)).\n> If, after we try all primes, `n > 1`, then `n` is a large prime. E.g. for 99951 example, the factorization looks like (33317 is a large prime):\n> ```\n> 99951: 3 33317\n> 33320: 2 2 2 5 7 7 17\n> 42: 2 3 7 1\n> 12: 2 2 3 1\n> 7: 7\n> ```\n\nWe try to each prime and `sum` the divisors, tracking the division result in `res`. If the `sum` equal `n`, then we take on `n`. If the `sum` is zero, `n` is the large prime, and we take on `n`.\n\nOthersise, we recurse on `sum`. Note that if `res > 1`, this is a large prime and we need to recurse on `sum + res`.\n\n**C++**\n```cpp\nint p[65] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97,\n 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199,\n 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313};\nint smallestValue(int n) {\n int sum = 0, res = n;\n for (int i = 0; i < 65 && res >= p[i]; ++i)\n while (res % p[i] == 0) {\n sum += p[i];\n res /= p[i];\n }\n return (sum == 0 || sum == n) ? n : smallestValue(sum + (res == 1 ? 0 : res));\n}\n``` | 13 | 2 | [] | 6 |
smallest-value-after-replacing-with-sum-of-prime-factors | Straight Forward || Easy || Reacursion|| Prime Factorization | straight-forward-easy-reacursion-prime-f-0umu | Base case: If n is prime it means we can not subdivide it, means it is in its minimum form hence return it.\n\nIf the sum_of_prime_factors_of_n == n then it me | pankaj_patel63 | NORMAL | 2022-12-18T05:39:03.876835+00:00 | 2023-02-22T04:33:12.063245+00:00 | 607 | false | Base case: If n is prime it means we can not subdivide it, means it is in its minimum form hence return it.\n\nIf the sum_of_prime_factors_of_n == n then it means we can not reduce it so we return n otherwise it will create infinite loop.\n\n# Code\n```\nclass Solution {\n bool isPrime(int n){\n for(int i = 2;i * i <= n;i++){\n if(n % i == 0)return false;\n }\n return true;\n }\npublic:\n int smallestValue(int n) {\n\n if(isPrime(n))return n;\n\n int x = n;\n int sum = 0;\n //Sum all the prime factors of n\n for(int i = 2;i * i <= n;i++){\n while(x % i == 0){\n sum += i;\n x /= i;\n }\n }\n if(x > 1)sum += x;\n\n if(sum == n)return n;\n\n return smallestValue(sum);\n }\n};\n```\nPlease Upvote if you found it helpfull\uD83D\uDE0A | 8 | 0 | ['Recursion', 'C'] | 2 |
smallest-value-after-replacing-with-sum-of-prime-factors | Very Simple Recursion | very-simple-recursion-by-kreakemp-hsqz | \n\n/*\nHere we use the theorem to optimize the runtime, that is prime no can be always in the form of 6k+1 or 6k-1, except 2 & 3\nSo we only itereate on 2, 3, | kreakEmp | NORMAL | 2022-12-18T04:01:42.814610+00:00 | 2022-12-18T04:01:42.814646+00:00 | 1,831 | false | ```\n\n/*\nHere we use the theorem to optimize the runtime, that is prime no can be always in the form of 6k+1 or 6k-1, except 2 & 3\nSo we only itereate on 2, 3, 6k-1 & 6k + 1\nWe have return a separate function that just keep on evluating the sum of all factor and return total sum.\n*/\nclass Solution {\npublic:\n int find(int n, int i, int j, bool b){ \n if(n <= i) return n;\n if(n%i == 0) {\n return i + find(n/i, i, j, b);\n }\n int t = 0;\n if(i == 2) t = 3;\n else{\n if(b) { t = j*6+1; j++; }\n else t = j*6-1;\n b = !b;\n }\n return find(n, t, j, b );\n }\n int smallestValue(int n) {\n while(1){\n int t = find(n, 2, 1, false);\n if(t == n) return n;\n n = t;\n }\n return 0;\n }\n};\n``` | 8 | 6 | ['C++'] | 2 |
smallest-value-after-replacing-with-sum-of-prime-factors | Beginner friendly Java Solution | beginner-friendly-java-solution-by-himan-djph | Code\n\nclass Solution {\n public int smallestValue(int n) {\n if(n == 0 || n == 4) return 4;\n return factors(n);\n }\n private int fac | HimanshuBhoir | NORMAL | 2022-12-18T04:09:09.105264+00:00 | 2022-12-18T04:09:09.105295+00:00 | 582 | false | # Code\n```\nclass Solution {\n public int smallestValue(int n) {\n if(n == 0 || n == 4) return 4;\n return factors(n);\n }\n private int factors(int n){\n int i, sum = 0;\n for(i=2; i<=n; i++){\n while(n%i == 0){\n sum += i;\n n /= i;\n }\n }\n return isPrime(sum) ? sum : factors(sum); \n }\n private boolean isPrime(int sum){\n for(int i=2; i<sum/2; i++){\n if(sum % i == 0) return false;\n }\n return true;\n }\n}\n``` | 7 | 0 | ['Java'] | 1 |
smallest-value-after-replacing-with-sum-of-prime-factors | [C++|Java|Python3] simulation | cjavapython3-simulation-by-ye15-b79g | Please pull this commit for solutions of weekly 324. \n\nIntuition\nWe can simply repeatedly compute the sum of prime factors of each number and see where it en | ye15 | NORMAL | 2022-12-18T04:03:58.319937+00:00 | 2022-12-18T23:08:51.706829+00:00 | 729 | false | Please pull this [commit](https://github.com/gaosanyong/leetcode/commit/af6e415cd101768ea8743ea9e4d22d788c9461c3) for solutions of weekly 324. \n\n**Intuition**\nWe can simply repeatedly compute the sum of prime factors of each number and see where it ends. \n**Implementation**\n**C++**\n```\nclass Solution {\npublic: \n\tint smallestValue(int n) {\n\t\twhile (true) {\n\t\t\tint sm = 0; \n\t\t\tfor (int f = 2, nn = n; f <= nn; ++f) \n\t\t\t\tfor (; nn % f == 0; nn /= f, sm += f); \n\t\t\tif (sm == n) break; \n\t\t\tn = sm;\n\t\t}\n\t\treturn n; \n\t}\n};\n```\n**Java**\n```\nclass Solution {\n\tpublic int smallestValue(int n) {\n\t\twhile (true) {\n\t\t\tint s = 0; \n\t\t\tfor (int f = 2, x = n; f <= x; ++f)\n\t\t\t\tfor (; x % f == 0; x /= f)\n\t\t\t\t\ts += f; \n\t\t\tif (s == n) break; \n\t\t\tn = s; \n\t\t}\n\t\treturn n; \n\t}\n}\n```\n**Python3**\n```\nclass Solution: \n\tdef smallestValue(self, n: int) -> int: \n\t\twhile True: \n\t\t\tnn, sm = n, 0\n\t\t\tfor f in range(2, nn+1): \n\t\t\t\twhile nn % f == 0: \n\t\t\t\t\tnn //= f \n\t\t\t\t\tsm += f\n\t\t\tif sm == n: break \n\t\t\tn = sm \n\t\treturn n\n```\n**Complexity**\nTime \nSpace O(1) | 7 | 0 | ['C', 'Java', 'Python3'] | 2 |
smallest-value-after-replacing-with-sum-of-prime-factors | [C++] Using Sieve | c-using-sieve-by-vgnshiyer-y4lc | \nclass Solution {\npublic:\n vector<int> sieve(int n){\n vector<int> isPrime(n+1, 1);\n isPrime[0] = isPrime[1] = 0;\n \n vector | vgnshiyer | NORMAL | 2022-12-18T04:01:24.487390+00:00 | 2022-12-18T04:01:24.487435+00:00 | 828 | false | ```\nclass Solution {\npublic:\n vector<int> sieve(int n){\n vector<int> isPrime(n+1, 1);\n isPrime[0] = isPrime[1] = 0;\n \n vector<int> prime;\n for(int i = 2; i <= n; i++){\n if(isPrime[i]){\n prime.push_back(i);\n for(long long j = (long long)i*i; j <= n; j += i) isPrime[j] = 0;\n }\n }\n return prime;\n }\n \n int smallestValue(int n) {\n /* generate prime numbers less than equal to n */\n vector<int> primes = sieve(n);\n \n while(true){\n int cur = n;\n int sum = 0;\n \n /* generate prime factors */\n for(int i = 0; i < primes.size() && primes[i]*primes[i] <= n; i++){\n while(n % primes[i] == 0){\n n /= primes[i];\n sum += primes[i];\n }\n }\n sum += (n != 1 ? n : 0); // if last number is prime itself, it cannot be furthur broken down -> add it\n\n if(sum == cur) return cur; // if number was not further decomposable, return it\n n = sum;\n }\n return -1; // never executes\n }\n};\n``` | 6 | 0 | ['C'] | 1 |
smallest-value-after-replacing-with-sum-of-prime-factors | ✳️Two Solution |Factorization is optimized using Sieve of Eratosthenes✅ | two-solution-factorization-is-optimized-bhxoy | Intuition\nWe know that product of numbers are always greater than sum of numbers, if the numbers are greater than 1. Hence, we repeatedly find the sum of the | tbekzhoroev | NORMAL | 2022-12-18T04:08:48.710915+00:00 | 2023-03-09T12:40:02.731514+00:00 | 587 | false | # Intuition\nWe know that product of numbers are always greater than sum of numbers, if the numbers are greater than 1. Hence, we repeatedly find the sum of the factors of a given number until the sum is itself. \n\n\n# Space Complexity: $\\mathcal{O}(1)$\n\n\n# Solution 1\n```\nclass Solution:\n def smallestValue(self, n: int) -> int:\n while n != (n:=self.factorization(n)) pass\n return n\n \n def factorization(self, n):\n factors_sum = 0\n i = 2\n while n > 1:\n while n % i == 0:\n n //= i\n factors_sum +=i\n i += 1\n return factors_sum\n \n \n```\n# Solution 2\nPre-compute a list of prime numbers up to the n using Sieve of Eratosthenes and store them. https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes. This algorithm is a fastest algorithm to find prime factors.\n\nThen use precomputed primes to avoid checking primality of the number during factorization. \n\n# Worst case Space Complexity: $\\mathcal{O}(\\sqrt{n})$\n# Code\n```\nclass Solution:\n def smallestValue(self, n: int) -> int:\n self.primes = self.sieve_of_eratosthenes(n)\n while n != (n:=self.factorization(n)) pass\n return n\n\n def factorization(self, n):\n factors = 0\n for prime in self.primes:\n while n % prime == 0:\n n //= prime\n factors += prime\n if n==1: break\n return factors\n \n def sieve_of_eratosthenes(self, n):\n primes = [True] * (n + 1)\n primes[0] = primes[1] = False\n for i in range(2, int(n ** 0.5) + 1):\n if primes[i]:\n for j in range(i ** 2, n + 1, i): primes[j] = False\n return [i for i in range(2, n + 1) if primes[i]]\n \n``` | 5 | 0 | ['Math', 'Python', 'Python3'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | C++ || Simple Prime Factorisation || EAsy to Understand | c-simple-prime-factorisation-easy-to-und-ds46 | \n# Code\n\nclass Solution {\npublic:\n int smallestValue(int n) {\n while(n){\n int sum = 0;\n int prev = n;\n for(i | pradipta_ltcode | NORMAL | 2022-12-18T04:08:47.786351+00:00 | 2022-12-18T04:08:47.786393+00:00 | 641 | false | \n# Code\n```\nclass Solution {\npublic:\n int smallestValue(int n) {\n while(n){\n int sum = 0;\n int prev = n;\n for(int i=2;i<=n;){\n if(n%i ==0){\n sum+=i;\n n/=i;\n }\n else{\n i++;\n }\n }\n if(prev == sum )return sum;\n if(sum == 0)return n;\n n=sum;\n }\n return 0;\n }\n};\n``` | 5 | 0 | ['C++'] | 2 |
smallest-value-after-replacing-with-sum-of-prime-factors | Simple java solution | simple-java-solution-by-siddhant_1602-w06r | \n\n# Code\n\nclass Solution {\n public int smallestValue(int n) {\n int k=n;\n if(n<=4)\n return n;\n\t\twhile(true)\n {\n | Siddhant_1602 | NORMAL | 2022-12-18T04:01:08.233023+00:00 | 2022-12-18T04:03:36.959435+00:00 | 1,325 | false | \n\n# Code\n```\nclass Solution {\n public int smallestValue(int n) {\n int k=n;\n if(n<=4)\n return n;\n\t\twhile(true)\n {\n int sum=0;\n for (int i=2;i<=n/2;i++)\n {\n //System.out.println(i);\n if(prime(i))\n {\n while(n%i==0)\n {\n sum+=i;\n n/=i;\n //System.out.println(i);\n }\n }\n\t\t }\n if(n==1)\n {\n sum-=1;\n }\n\t\t sum+=n;\n\t\t if(n==sum)\n\t\t {\n k=sum;\n break;\n\t\t }\n\t\t else\n\t\t {\n n=sum;\n k=sum;\n\t\t }\n }\n\t\treturn k;\n }\n public boolean prime(int n)\n {\n for(int i=2;i<=Math.sqrt(n);i++)\n {\n if(n%i==0)\n {\n return false;\n }\n }\n return true;\n }\n}\n``` | 5 | 1 | ['Java'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | Java | Simplest solution | beats 100% | java-simplest-solution-beats-100-by-akxh-cj5p | Approach\n Describe your approach to solving the problem. \n1. If a number is prime, that is the smallest value itself.\n2. Else Calculate the sum of prime fact | akxhayxharma | NORMAL | 2022-12-18T04:01:05.513982+00:00 | 2022-12-19T03:26:36.229093+00:00 | 1,801 | false | # Approach\n<!-- Describe your approach to solving the problem. -->\n1. If a number is prime, that is the smallest value itself.\n2. Else Calculate the sum of prime factors\n - Calculate first prime factor.\n - Add in sum\n - Calculate remainder\n - Reapeat above steps till remainder is prime.\n - Add remainder in sum\n3. if sum is equal to input, it will run indefinitely, so we return the value.\n4. Else we will repeat All above steps for the sum.\n\n# Complexity\n- Time complexity: O(nlogn) (Always better than this)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n public int smallestValue(int n) {\n if(isPrime(n)) return n; // otherwise it will run forever\n int sum = getPrimeFactorSum(n);\n if(sum == n) return n; // otherwise it will run forever\n return smallestValue(sum);\n }\n \n public boolean isPrime(int n) { // to check if number is prime\n if(n == 2) return true;\n for(int i = 2; i < Math.sqrt(n) + 1; i++) {\n if(n % i == 0) return false;\n }\n return true;\n }\n \n public int getFirstPrimeFactor(int n) { // to get first prime number\n if(isPrime(n)) return n;\n for(int i = 2; i < n; i++) {\n if(n % i == 0) return i;\n }\n return n;\n }\n\n public int getPrimeFactorSum(int n) { //sum of prime factors of a number\n int sum = 0;\n while(!isPrime(n)) {\n int m = getFirstPrimeFactor(n);\n n /= m;\n sum += m;\n }\n sum += n;\n return sum;\n }\n \n}\n``` | 5 | 0 | ['Java'] | 2 |
smallest-value-after-replacing-with-sum-of-prime-factors | Beats 100% || Easy and Simple Solution🔥🔥 | beats-100-easy-and-simple-solution-by-_j-udez | Intuition\nBasic intution is find the sum of only those factors which are prime and then replacing it with the initial "n" till the number "n" have no factor ot | _jarvis | NORMAL | 2023-12-21T08:00:45.403193+00:00 | 2023-12-21T08:09:40.969745+00:00 | 69 | false | # Intuition\nBasic intution is find the sum of only those factors which are prime and then replacing it with the initial "n" till the number "n" have no factor other than 1 and "n", i.e. till "n" becomes prime.\n\n**Every line of code is explained through comments**\n\n\n# Code\n```\nclass Solution {\npublic:\nbool isPrime(int n) // Function to find whether the number is prime or not\n{\n if(n<=1) return false;\n for(int i = 2; i<=sqrt(n); i++) //If you have doubt why i have run till sqrt(n) then go through the link [https://www.geeksforgeeks.org/why-do-we-check-up-to-the-square-root-of-a-number-to-determine-if-that-number-is-prime/]\n {\n if(n%i==0) return false;\n }\n return true;\n}\n\n\n// Function to return sum of all the factors of "n" that are prime.\nint find(int n)\n{\n int sum =0, i=2;\n while(n!=1)\n {\n if(n%i==0)\n {\n if(isPrime(i)) // If the factor is prime then we will add it to sum.\n {\n sum+=i;\n }\n n/=i;\n i=1;\n // after finding a factor we will again find the next factor starting from 2.\n \n }\n i++;\n }\n return sum;\n}\n int smallestValue(int n) {\n\n //BASE CASE\n// Why I have taken 1 and 4 as base Case ?\n// --> The factors of 4 are 2 and 2 that are prime whose sum is 4,\n// and we are replacing n with sum , which will cause an infinite loop.\n// Similarly with case of 1.\n if(n==1) return 1;\n if(n==4) return 4;\n \n\n // Replacing the number "n" with the sum of prime factors,till the\n // number have only 1 and "n" as factor, i.e. till it become prime.\n while(!(isPrime(n)))\n {\n int rep = find(n);\n n=rep;\n }\n\n return n;\n\n }\n};\n``` | 4 | 0 | ['C++'] | 1 |
smallest-value-after-replacing-with-sum-of-prime-factors | C++ || MATHS | c-maths-by-ganeshkumawat8740-z3mk | Code\n\nclass Solution {\npublic:\n int smallestValue(int n) {\n int x ,k,i;\n while(n){\n x = 0;\n k = n;\n f | ganeshkumawat8740 | NORMAL | 2023-07-30T08:57:47.505785+00:00 | 2023-07-30T08:57:47.505812+00:00 | 216 | false | # Code\n```\nclass Solution {\npublic:\n int smallestValue(int n) {\n int x ,k,i;\n while(n){\n x = 0;\n k = n;\n for(i = 2; i <= sqrt(n); i++){\n while(n%i == 0){\n x += i;\n n /= i;\n }\n }\n if(n>1)x += n;\n if(k == x)return x;\n n = x;\n }\n return x;\n }\n};\n``` | 4 | 0 | ['Math', 'Number Theory', 'C++'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | C++ || Commented-Code | c-commented-code-by-sourabcodes-1yfv | class Solution {\npublic:\n \n //* Function to create a hash array for prime numbers till n *.\n vector hashprime(int n){\n vector arr(n,0);\n\n | SourabCodes | NORMAL | 2022-12-18T19:19:23.710811+00:00 | 2022-12-18T19:19:23.710839+00:00 | 144 | false | class Solution {\npublic:\n \n //**** Function to create a hash array for prime numbers till n ****.\n vector<int> hashprime(int n){\n vector<int> arr(n,0);\n\n for(int i = 0 ; i < n ; i++){\n arr[i] = i;\n }\n arr[0] = arr[1] = 0;\n for(int i = 2 ; i < n ; i++){\n if(arr[i] != 0){\n for(int ind = i+i; ind < n ; ind += i){\n arr[ind] = 0;\n }\n }\n }\n\n return arr;\n }\n \n int smallestValue(int n) {\n int ans = n;\n \n vector<int> hash = hashprime(n+1); \n \n // while hash is not a prime number we will iterate.\n \n while(!hash[ans]){\n \n int newno = 0; // Variable to store the sum of prime factors.\n \n // storing current number to maintain the for loop in next line.\n int sz = ans;\n \n for(int i = 2 ; i <= sz/2; i++){\n while(ans%i == 0){\n newno += i;\n ans /= i;\n }\n }\n \n // if the number is equal to the sum of its prime factors then return in order to avoid infinite loop.\n if(sz == newno)\n return newno;\n \n // replace original number with sum of its prime factors.\n \n ans = newno;\n \n }\n \n return ans;\n }\n}; | 4 | 0 | ['C'] | 1 |
smallest-value-after-replacing-with-sum-of-prime-factors | Simple || C++ | simple-c-by-shrikanta8-xy2j | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. \n\n\n\n\n\n\n\n \n\n# Code\n\nclass Solution {\npublic:\n int primeFactorsFin | Shrikanta8 | NORMAL | 2022-12-18T04:02:03.187600+00:00 | 2022-12-18T04:03:34.900199+00:00 | 397 | false | <!-- # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n<!-- # Approach -->\n<!-- Describe your approach to solving the problem. -->\n\n<!-- # Complexity\n- Time complexity: -->\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n<!-- - Space complexity:\nAdd your space complexity here, e.g. $$O(n)$$ --> \n\n# Code\n```\nclass Solution {\npublic:\n int primeFactorsFind(int n)\n {\n int newNum=0;\n while (n % 2 == 0)\n {\n newNum += 2;\n n = n/2;\n }\n\n \n for (int i = 3; i <= sqrt(n); i = i + 2)\n {\n \n while (n % i == 0)\n {\n newNum += i;\n n = n/i;\n }\n }\n\n if (n > 2)\n newNum += n;\n return newNum;\n }\n \n bool isPrimeNum(int n)\n {\n \n for (int i = 2; i * i <= n; i++) {\n if (n % i == 0)\n return false;\n }\n return true;\n }\n \n int smallestValue(int n) {\n \n unordered_set<int> st;\n \n while(1){\n \n st.insert(n);\n \n //checking wheteher n is prime or not\n if(isPrimeNum(n)) \n return n;\n \n //finding sum of it\'s prime factors\n n = primeFactorsFind(n);\n \n //checking whether value of n is repeating or not\n if(st.find(n) != st.end()){\n return n;\n }\n \n }\n return n;\n }\n};\n\n``` | 4 | 0 | ['C++'] | 1 |
smallest-value-after-replacing-with-sum-of-prime-factors | Java Solution | java-solution-by-kthnode-b34e | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | kthNode | NORMAL | 2023-05-28T06:35:56.240427+00:00 | 2023-05-28T06:35:56.240468+00:00 | 410 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n public int smallestValue(int n) {\n int k=n;\n if(n<=4)\n return n;\n\t\twhile(true)\n {\n int sum=0;\n for (int i=2;i<=n/2;i++)\n {\n //System.out.println(i);\n if(prime(i))\n {\n while(n%i==0)\n {\n sum+=i;\n n/=i;\n //System.out.println(i);\n }\n }\n\t\t }\n if(n==1)\n {\n sum-=1;\n }\n\t\t sum+=n;\n\t\t if(n==sum)\n\t\t {\n k=sum;\n break;\n\t\t }\n\t\t else\n\t\t {\n n=sum;\n k=sum;\n\t\t }\n }\n\t\treturn k;\n }\n public boolean prime(int n)\n {\n for(int i=2;i<=Math.sqrt(n);i++)\n {\n if(n%i==0)\n {\n return false;\n }\n }\n return true;\n }\n}\n``` | 3 | 0 | ['Java'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | c++ || easy solution using prime factarization and seive | c-easy-solution-using-prime-factarizatio-waa3 | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | aashi__70 | NORMAL | 2023-01-30T19:05:04.338463+00:00 | 2023-01-30T19:05:04.338502+00:00 | 86 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n\nint n=1e5;\nvector<int> prime(n,0);\nclass Solution {\npublic:\nvoid fun(){\nfor(int i=2;i*i<n;i++){\n if(prime[i]==0){\nfor(int j=i*i;j<n;j+=i){\n prime[j]=1;\n}\n }\n}\n}\nint primefactor(int n){\n int sum=0;\n for(int i=2;i<=sqrt(n);i++){\n while(n%i==0){\n sum+=i;\n n/=i;\n }\n }\n if(n>1) \n sum+=n;\n \n \n return sum;\n}\n\n int smallestValue(int n) {\n int sum=0;\n fun();\n while(1){\n sum=primefactor(n);\n if(prime[sum]==0) return sum;\n if(n==sum) break;\n n=sum;\n }\n \n return n;\n }\n};\n``` | 3 | 0 | ['C++'] | 1 |
smallest-value-after-replacing-with-sum-of-prime-factors | using prime factor | using-prime-factor-by-lavkush_173-ukuh | Intuition\nUsing prime factore\n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time complexity:\n Add your time complexity he | Lavkush_173 | NORMAL | 2022-12-20T18:13:38.728902+00:00 | 2022-12-20T18:13:38.728941+00:00 | 127 | false | # Intuition\nUsing prime factore\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n public int smallestValue(int n) {\n int x=n;\n int sum=0,j=0;\n if(n<=5){\n return n;\n }\n // if(n==4) return \n while(true){\n int i=2;\n sum=0;\n while (n>1) {\n if(n%i==0){\n sum+=i;\n n=n/i;\n }\n else i++;\n }\n n=sum;\n if(sum==x) break;\n else x=sum;\n }\n return x;\n }\n}\n``` | 3 | 0 | ['Java'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | Check Prime | check-prime-by-ayushy_78-ti52 | # Intuition \n\n\n\n\n\n# Complexity\n- Time complexity: O((log n) ^ 2)\n\n\n- Space complexity: O(log n)\n\n\n# Code\n\nclass Solution {\npublic:\n bool i | SelfHelp | NORMAL | 2022-12-18T04:50:42.229427+00:00 | 2022-12-18T04:50:42.229451+00:00 | 137 | false | <!-- # Intuition -->\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n<!-- # Approach -->\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: *`O((log n) ^ 2)`*\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: *`O(log n)`*\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n bool isPrime(int n) {\n int sqr = sqrt(n);\n for(int i = 2; i <= sqr; i++)\n if(n % i == 0)\n return false;\n return true;\n }\n int smallestValue(int n) {\n if(isPrime(n))\n return n;\n int sum = 0, N = n;\n for(int i = 2; i <= n; i++) {\n while(n % i == 0) {\n n /= i;\n sum += i;\n }\n }\n if(sum == N)\n return N;\n return smallestValue(sum);\n }\n};\n``` | 3 | 0 | ['Math', 'Simulation', 'C++'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | ✅ C++ || Simple Solution || Easy Solution | c-simple-solution-easy-solution-by-indre-5fiy | \nclass Solution {\npublic:\n vector<int> findPrimeFactors(int n) {\n vector<int> prime_factors;\n // 2 is the smallest prime number\n while (n % 2 | indresh149 | NORMAL | 2022-12-18T04:03:59.929917+00:00 | 2022-12-18T04:03:59.929973+00:00 | 1,147 | false | ```\nclass Solution {\npublic:\n vector<int> findPrimeFactors(int n) {\n vector<int> prime_factors;\n // 2 is the smallest prime number\n while (n % 2 == 0) {\n prime_factors.push_back(2);\n n = n / 2;\n }\n // check for other prime numbers up to sqrt(n)\n for (int i = 3; i <= sqrt(n); i += 2) {\n while (n % i == 0) {\n prime_factors.push_back(i);\n n = n / i;\n }\n }\n // if n is a prime number itself, add it to the list\n if (n > 2) {\n prime_factors.push_back(n);\n }\n return prime_factors;\n}\n int smallestValue(int n) {\n while (true) {\n vector<int> prime_factors = findPrimeFactors(n);\n int new_n = 0;\n for (int i : prime_factors) {\n new_n += i;\n }\n if (new_n == n) {\n return n;\n } else {\n n = new_n;\n }\n }\n }\n};\n```\n**Please upvote if it was helpful for you, thank you!** | 3 | 0 | ['C'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | Python3 | Finding prime factors | Intuitive | Easy to understand | python3-finding-prime-factors-intuitive-1tcf6 | \n# Code\n\nclass Solution:\n def prime_factors(self, n):\n i = 2\n factors = []\n while i * i <= n:\n if n % i:\n | jubinsoni | NORMAL | 2022-12-18T04:02:01.658476+00:00 | 2022-12-18T04:02:01.658518+00:00 | 886 | false | \n# Code\n```\nclass Solution:\n def prime_factors(self, n):\n i = 2\n factors = []\n while i * i <= n:\n if n % i:\n i += 1\n else:\n n //= i\n factors.append(i)\n if n > 1:\n factors.append(n)\n return factors\n \n def smallestValue(self, n: int) -> int:\n num = n\n seen = set()\n while num > 0:\n factors = self.prime_factors(num)\n if len(factors) == 1:\n return factors[0]\n num = sum(factors)\n if num in seen:\n break\n seen.add(num)\n return num\n \n \n \n``` | 3 | 0 | ['Math', 'Python3'] | 1 |
smallest-value-after-replacing-with-sum-of-prime-factors | Easy cpp | easy-cpp-by-psetti-f9nc | \nclass Solution {\npublic:\n int smallestValue(int n) {\n int primes = primeFactors(n);\n if(primes == n) {\n return n;\n } | psetti | NORMAL | 2022-12-18T04:00:58.557573+00:00 | 2022-12-18T04:00:58.557621+00:00 | 1,411 | false | ```\nclass Solution {\npublic:\n int smallestValue(int n) {\n int primes = primeFactors(n);\n if(primes == n) {\n return n;\n }\n return smallestValue(primes);\n }\n int primeFactors(int n) {\n int result = 0;\n while (n % 2 == 0)\n {\n result += 2;\n n = n/2;\n }\n for (int i = 3; i <= sqrt(n); i = i + 2)\n {\n while (n % i == 0)\n {\n result += i;\n n = n/i;\n }\n }\n if(n > 2) {\n result += n;\n }\n return result;\n}\n \n};\n``` | 3 | 0 | ['Math'] | 1 |
smallest-value-after-replacing-with-sum-of-prime-factors | ✔[C++] Simple Solution | c-simple-solution-by-in-imitable-n2yr | Code\n\nclass Solution {\npublic:\n int solve(int n){\n if(n<=3) return n;\n int sum=0;\n for(int i=2; i<=n; i++){\n while(n% | in-imitable | NORMAL | 2022-12-18T07:12:44.636629+00:00 | 2022-12-18T07:12:44.636656+00:00 | 159 | false | # Code\n```\nclass Solution {\npublic:\n int solve(int n){\n if(n<=3) return n;\n int sum=0;\n for(int i=2; i<=n; i++){\n while(n%i == 0){\n sum += i;\n n = n/i;\n }\n }\n return sum;\n }\n int smallestValue(int n) {\n int result = solve(n);\n while(true){\n int ans = solve(result);\n if(ans < result) result = ans;\n else return result;\n }\n return 0;\n }\n};\n``` | 2 | 0 | ['C++'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | python3 Solution | python3-solution-by-motaharozzaman1996-ftvj | \n\nclass Solution:\n def smallestValue(self, n: int) -> int:\n while True:\n c=2\n t=0\n b=n\n\n while c* | Motaharozzaman1996 | NORMAL | 2022-12-18T04:14:15.715013+00:00 | 2022-12-18T04:14:15.715060+00:00 | 116 | false | \n```\nclass Solution:\n def smallestValue(self, n: int) -> int:\n while True:\n c=2\n t=0\n b=n\n\n while c*c<=n:\n while n%c==0:\n n//=c\n t+=c\n\n c+=1\n\n if t==0:\n return n\n\n if t>=b:\n return b\n\n if n>1:\n t+=n\n\n n=t \n``` | 2 | 0 | ['Python3'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | ✅ Easy and simple || recursive || C++ | easy-and-simple-recursive-c-by-mohitk30-6pub | \n\nclass Solution {\npublic:\n int f(int n)\n{\n int sm=0;\n while (n % 2 == 0)\n {\n // cout << 2 << " ";\n sm+=2;\n n = n/2; | mohitk30 | NORMAL | 2022-12-18T04:01:06.585277+00:00 | 2022-12-18T04:01:06.585320+00:00 | 1,136 | false | \n```\nclass Solution {\npublic:\n int f(int n)\n{\n int sm=0;\n while (n % 2 == 0)\n {\n // cout << 2 << " ";\n sm+=2;\n n = n/2;\n }\n \n \n for (int i = 3; i <= sqrt(n); i = i + 2)\n {\n \n while (n % i == 0)\n {\n // cout << i << " ";\n sm+=i;\n n = n/i;\n }\n }\n \n \n if (n > 2)\n // cout << n << " ";\n sm+=n;\n \n \n return sm;\n}\n int smallestValue(int n) {\n int ans=f(n);\n if(ans==n) return ans;\n else return smallestValue(ans);\n \n }\n};\n``` | 2 | 0 | ['C++'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | Recursive solution with factorization, Beats 100% | recursive-solution-with-factorization-be-3alz | Code | simolekc | NORMAL | 2025-03-03T16:24:06.142152+00:00 | 2025-03-03T16:24:06.142152+00:00 | 43 | false |
# Code
```javascript []
/**
* @param {number} n
* @return {number}
*/
var smallestValue = function (n) {
let result = n;
let newResult = 0;
let divisor = 2;
while (n > 1) {
while (n % divisor === 0) {
newResult += divisor;
n /= divisor;
}
divisor++;
}
if (newResult < result) {
return smallestValue(newResult);
}
return result;
};
``` | 1 | 0 | ['JavaScript'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | (Java) || Time complexity: O(log n . √n) || Runtime : 1ms | java-time-complexity-olog-n-n-runtime-1m-qtdp | IntuitionThe problem requires repeatedly reducing a number 𝑛 by replacing it with the sum of its prime factors until it becomes a prime number. The key observat | shikhargupta0645 | NORMAL | 2025-01-22T01:40:21.653395+00:00 | 2025-01-22T01:40:21.653395+00:00 | 41 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem requires repeatedly reducing a number 𝑛 by replacing it with the sum of its prime factors until it becomes a prime number. The key observation is that during each iteration, 𝑛 gets smaller (or stabilizes) due to the summation process, eventually converging to a prime number.
# Approach
<!-- Describe your approach to solving the problem. -->
Prime Factorization:
Decompose 𝑛 into its prime factors and calculate their sum. This is done by iterating from 2 to 𝑛 and dividing 𝑛 by each factor until it becomes indivisible.
Repeat this process until 𝑛 is a prime number.
Prime Check:
Use a helper function isPrime to check whether a number is prime by iterating up to 𝑛.
Termination Condition:
If the sum of the prime factors equals 𝑛 (i.e., no further reduction is possible), return 𝑛.
Otherwise, update 𝑛 to the computed sum and repeat.
# Complexity
- Time complexity: O(log n . √n)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity: O(1)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```java []
class Solution {
public int smallestValue(int n) {
while(! isPrime(n)){
int sum = 0;
int temp = n;
for(int i=2; i<=temp; i++){
while(temp%i == 0){
sum += i;
temp /= i;
}
}
if(sum == n){
return n;
}
n = sum;
}
return n;
}
public Boolean isPrime(int n){
if(n<2){
return false;
}
for(int i=2; i*i<=n; i++){
if(n%i == 0){
return false;
}
}
return true;
}
}
``` | 1 | 0 | ['Java'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | easy solution using recursion and maths | easy-solution-using-recursion-and-maths-zio3u | \n\n# Complexity\n- Time complexity:\n Add your time complexity here, e.g. O(n) \n\n- Space complexity:\n Add your space complexity here, e.g. O(n) \n\n# Code\n | ujjuengineer | NORMAL | 2024-10-07T00:57:18.502712+00:00 | 2024-10-07T00:57:18.502750+00:00 | 23 | false | \n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n\n // function for checking prime\n bool isPrime(int n) {\n if(n==1) return false;\n for(int i = 2; i <= sqrt(n); i++) {\n if(n%i == 0) return false;\n }\n return true;\n }\n\n int smallestValue(int n) {\n if(isPrime(n)) return n;\n int sum = 0;\n \n // adding prime factors before sqrt(n)\n for(int i = 2; i < sqrt(n); i++) {\n if(n%i == 0 && isPrime(i)) {\n int m = n;\n while(m % i == 0) {\n sum += i;\n m /= i;\n }\n }\n }\n // adding prime factor after sqrt(n)\n for(int i = 2; i <= sqrt(n); i++) {\n if(n % (n/i) == 0 && isPrime(n/i)) {\n int m = n;\n while(m % (n/i) == 0) {\n sum += (n/i);\n m /= (n/i);\n }\n }\n }\n \n if(sum == n) return n; // special case when n = 4\n return smallestValue(sum);\n }\n};\n``` | 1 | 0 | ['C++'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | easiest approch you can get (beginner friendly) | easiest-approch-you-can-get-beginner-fri-stoj | Intuition\n\nThe problem involves finding a transformation of a number based on the sum of its prime factors. The idea is to continuously replace the number wit | ishanbagra | NORMAL | 2024-08-04T06:00:05.014828+00:00 | 2024-08-04T06:00:05.014887+00:00 | 6 | false | Intuition\n\nThe problem involves finding a transformation of a number based on the sum of its prime factors. The idea is to continuously replace the number with the sum of its prime factors until the value no longer changes. This essentially reduces the number by breaking it down into smaller prime components and summing them.\n\nApproach\n Prime Checking: A helper function is_prime determines if a given number is prime. It checks divisibility up to sqrt(n) for optimization.\n Prime Factorization: The function primefactor calculates the sum of all prime factors of n. It iterates over potential factors, checking if they are prime and if they divide n.\n Transformation: The function smallestValue repeatedly calls primefactor on n and updates n to the result until the value stabilizes (i.e., the sum of prime factors equals the original number).\n\nComplexity\nTime Complexity:\n The is_prime function has a time complexity of O(n)(n\u200B).\nThe primefactor function iterates over the factors up to nn\n\u200B, and in the worst case, it may reduce the number logarithmically each time, leading to a complexity of O(nlog\u2061n)O(n\u200Blogn) per call.\nThe smallestValue function may call primefactor logarithmically many times based on the prime factors sum reductions, resulting in an overall complexity of O(nlog\u20612n)O(nlog2n).\n\nSpace Complexity:\n\nThe space complexity is O(1)O(1) as no extra space proportional to the input size is required.\n\n# Code\n```\nclass Solution {\npublic:\nbool is_prime(int n)\n{\n if(n==1)\n {\n return false;\n }\n else\n {\n for(int i=2;i<=sqrt(n);i++)\n {\n if(n%i==0)\n {\n return false;\n }\n }\n }\n return true;\n}\n\n\n\nint primefactor(int n)\n{\n if(is_prime(n))\n {\n return n;\n }\n int sml=0;\n int i=1;\n while(n>1)\n {\n if(is_prime(i) && n%i==0)\n {\n n=n/i;\n sml=sml+i;\n if(n%i==0)\n {\n i=i;\n }\n else\n {\n i++;\n }\n }\n else\n {\n i++;\n }\n }\n return sml;\n}\n int smallestValue(int n) {\n while (true) {\n int sum = primefactor(n);\n if (sum == n) break; // No further reduction possible\n n = sum;\n }\n return n;\n}\n};\n``` | 1 | 0 | ['Math', 'Number Theory', 'C++'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | C++ || JavaScript || Easy Solution | c-javascript-easy-solution-by-imran32-znba | Complexity\n- Time complexity: O(sqrt(n))\n\n- Space complexity: O(1)\n\n\n# Code\n# C++\n\nclass Solution {\npublic:\n int findAns(int n){\n int div= | Imran32 | NORMAL | 2023-08-17T13:51:22.266306+00:00 | 2023-08-17T13:51:22.266330+00:00 | 335 | false | # Complexity\n- Time complexity: O(sqrt(n))\n\n- Space complexity: O(1)\n\n\n# Code\n# C++\n```\nclass Solution {\npublic:\n int findAns(int n){\n int div=2,ans=0;\n while(n%2==0){\n ans+=2;\n n/=2;\n }\n int res=sqrt(n);\n for(int i=3;i<=res;i+=2){\n while(n%i==0){\n ans+=i;\n n/=i;\n }\n }\n if(n>2) ans+=n;\n return ans;\n }\n int smallestValue(int n) {\n while(1){\n int ans=findAns(n);\n if(ans==n) return n;\n n=ans;\n }\n return n;\n }\n};\n```\n\n---\n\n# JavaScript\n```\n/**\n * @param {number} n\n * @return {number}\n */\nvar smallestValue = function(n) {\n const findAns=(n)=>{\n let ans=0;\n while(n%2==0){\n ans+=2;\n n/=2;\n }\n let res=Math.sqrt(n);\n for(let i=3;i<=res;i+=2){\n while(n%i==0){\n ans+=i;\n n/=i;\n }\n }\n if(n>2) ans+=n;\n return ans;\n }\n while(1){\n let ans=findAns(n);\n if(ans==n) return n;\n n=ans;\n }\n return n;\n};\n``` | 1 | 0 | ['Math', 'Number Theory', 'C++', 'JavaScript'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | simple maths || easy to understand | simple-maths-easy-to-understand-by-aksha-vpil | Code\n\nclass Solution {\npublic:\n int smallestValue(int n) \n {\n while(true)\n {\n int sum = getsum(n);\n\n if( | akshat0610 | NORMAL | 2023-04-18T19:04:41.335046+00:00 | 2023-04-18T19:04:41.335085+00:00 | 213 | false | # Code\n```\nclass Solution {\npublic:\n int smallestValue(int n) \n {\n while(true)\n {\n int sum = getsum(n);\n\n if(sum == n)\n return n;\n\n n= sum;\n } \n return -1;\n }\n int getsum(int n)\n {\n int sum = 0;\n for(int i=2;i*i<=n;i++)\n {\n while((n%i) == 0)\n {\n sum = sum + i;\n n = n /i;\n }\n }\n if(n > 1) sum = sum + n;\n \n return sum;\n }\n};\n``` | 1 | 0 | ['Math', 'Number Theory', 'C++'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | Easy C++ CODE || beats 100% | easy-c-code-beats-100-by-sattvikdwivedi-elpj | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | sattvikdwivedi | NORMAL | 2023-01-14T21:29:47.386345+00:00 | 2023-01-14T21:29:47.386375+00:00 | 31 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\nint primeFactors(int n ,int ans)\n{\n int n1=n;\n while (n % 2 == 0)\n {\n ans+=2;\n n = n/2;\n }\n for (int i = 3; i <= sqrt(n); i = i + 2)\n {\n while (n % i == 0)\n {\n ans +=i;\n n = n/i;\n }\n }\n \n if (n > 2){\n ans+=n;}\n if(ans>=n1){\n return n1 ;\n }\n else{\n cout<<ans;\n return primeFactors(ans,0);\n }\n}\n int smallestValue(int n) {\n int ans=0;\n\n return primeFactors( n, ans);\n // return ans;\n }\n};\n``` | 1 | 0 | ['C++'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | C++ || Beats 80% || Recursion || Prime Factor | c-beats-80-recursion-prime-factor-by-gar-xohf | \n# Code\n\nclass Solution {\npublic:\n /* intution : Recursion\n base case\n if(isPrime(n))\n return n;\n else\n n | garvit14 | NORMAL | 2023-01-08T11:40:52.179498+00:00 | 2023-01-08T11:40:52.179530+00:00 | 133 | false | \n# Code\n```\nclass Solution {\npublic:\n /* intution : Recursion\n base case\n if(isPrime(n))\n return n;\n else\n n tak k prime factor nekal le\n eg 15 - 2,3,5,7,11,13 -> yaha se calculate newN\n */\n bool isPrime(int n){\n for(int i = 2;i * i <= n;i++){\n if(n % i == 0)return false;\n }\n return true;\n} \n int solve(int n)\n {\n // base case\n if(isPrime(n)){\n return n;\n }\n vector<int> temp;\n int d=2;\n int x=n;\n while(x>1)\n {\n if(x%d==0)\n {\n temp.push_back(d);\n x=x/d;\n }\n else\n d++;\n }\n\n int newN=0;\n for(int i=0;i<temp.size();i++)\n newN+=temp[i];\n \n if(newN == n)\n return n;\n return solve(newN);\n }\n int smallestValue(int n) {\n return solve(n);\n }\n};\n``` | 1 | 0 | ['Recursion', 'C++'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | [Python3], Recursevily sum primes | python3-recursevily-sum-primes-by-yerkon-55ul | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | yerkon | NORMAL | 2023-01-02T09:48:30.553186+00:00 | 2023-01-03T05:51:25.985180+00:00 | 31 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def smallestValue(self, n: int) -> int:\n while ((r := self.sumPrimes(n)) != n):\n n = r\n\n return n\n\n def sumPrimes(self, n: int) -> int:\n sum = 0\n for k in range(2, n + 1):\n if (n % k == 0):\n sum += k + self.sumPrimes(n // k)\n break\n\n return sum\n``` | 1 | 0 | ['Recursion', 'Python3'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | C++ || EASY TO UNDERSTAND || 💯 | c-easy-to-understand-by-__rushimanurkar-80sz | \nclass Solution {\npublic:\n bool checkPrime(int n){\n if(n<=1)\n return false;\n if(n==2 || n==3)\n return true;\n | __rushiManurkar_ | NORMAL | 2022-12-30T09:51:47.732620+00:00 | 2022-12-30T09:51:47.732662+00:00 | 23 | false | ```\nclass Solution {\npublic:\n bool checkPrime(int n){\n if(n<=1)\n return false;\n if(n==2 || n==3)\n return true;\n if(n%2==0 || n%3==0)\n return false;\n for(int i=5;i*i<=n;i+=6){\n if(n%i==0 || n%(i+2)==0)\n return false;\n }\n return true;\n }\n int smallestValue(int n) {\n if(n==4){\n return 4;\n }\n while(checkPrime(n)==false){\n int sum=0;\n while(n%2==0){\n sum+=2;\n n/=2;\n }\n for(int i=3;i<=sqrt(n);i++){\n while(n%i==0){\n sum+=i;\n n/=i;\n }\n }\n if(checkPrime(n))\n sum+=n;\n // cout<<sum<<endl;\n n=sum;\n }\n return n;\n }\n};\n``` | 1 | 0 | [] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | Smallest-value-after-replacing-with-sum-of-prime-factors||C++||Solution | smallest-value-after-replacing-with-sum-v3aa1 | Runtime: 3 ms, faster than 74.91% of C++ online submissions for Smallest Value After Replacing With Sum of Prime Factors.\nMemory Usage: 5.9 MB, less than 72.81 | SHIV_PRAKASH_YADAV | NORMAL | 2022-12-26T19:51:42.377488+00:00 | 2022-12-26T19:54:32.640438+00:00 | 33 | false | Runtime: 3 ms, faster than 74.91% of C++ online submissions for Smallest Value After Replacing With Sum of Prime Factors.\nMemory Usage: 5.9 MB, less than 72.81% of C++ online submissions for Smallest Value After Replacing With Sum of Prime Factors.\n```\n\nint prime(int num)\n{\n int x=num;\n int sum=0;\n for(int i=2;x>1;i++)\n {\n while(x%i==0)\n {\n sum=sum+i;\n x=x/i;\n }\n }\n return sum;\n}\n\n\n\nclass Solution {\npublic:\n int smallestValue(int n) \n {\n int sum=prime(n);\n if(sum==n)\n {\n return sum;\n }\n else \n {\n return smallestValue(sum);\n }\n \n }\n};\n``` | 1 | 0 | ['C'] | 1 |
smallest-value-after-replacing-with-sum-of-prime-factors | easiest solution || c++ || easy to understand | easiest-solution-c-easy-to-understand-by-4s47 | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | youdontknow001 | NORMAL | 2022-12-25T14:15:48.591903+00:00 | 2022-12-25T14:15:48.591949+00:00 | 66 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n vector<int> primeFactors(int n){\n vector<int> ans;\n while (n % 2 == 0){\n ans.push_back(2);\n n = n/2;\n }\n for (int i = 3; i <= sqrt(n); i = i + 2){\n while (n % i == 0){\n ans.push_back(i);\n n = n/i;\n }\n }\n if (n > 2) ans.push_back(n);\n return ans;\n }\n int smallestValue(int n) {\n if(n<6) return n;\n vector<int> temp;\n temp = primeFactors(n);\n n = accumulate(temp.begin(),temp.end(),0);\n while(n>0 && temp.size()>1){\n temp.clear();\n temp=primeFactors(n);\n n=accumulate(temp.begin(),temp.end(),0);\n }\n return n;\n }\n};\n``` | 1 | 0 | ['C++'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | C++||ContestSolution||EasyToUnderstand | ccontestsolutioneasytounderstand-by-ravi-62oh | Runtime: 3 ms, faster than 75.04% of C++ online submissions for Smallest Value After Replacing With Sum of Prime Factors.\nMemory Usage: 5.9 MB, less than 95.21 | ravispandey_98670 | NORMAL | 2022-12-25T10:43:12.982333+00:00 | 2022-12-25T10:43:12.982364+00:00 | 507 | false | Runtime: 3 ms, faster than 75.04% of C++ online submissions for Smallest Value After Replacing With Sum of Prime Factors.\nMemory Usage: 5.9 MB, less than 95.21% of C++ online submissions for Smallest Value After Replacing With Sum of Prime Factors.\n\nclass Solution {\npublic:\n \n int primeFactor(int n){\n int sum=0;\n for(int i=2;n>1;i++){\n while(n%i==0){\n sum=sum+i;\n n=n/i;\n }\n }\n return sum;\n }\n int smallestValue(int n) {\n \n int sum=primeFactor(n);\n if(sum == n)\n return sum;\n else\n return smallestValue(sum); \n\t\t\t\n }\n}; | 1 | 0 | ['Math', 'C'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | EASY C++ | easy-c-by-kalamarham-3kjp | Intuition\n Describe your first thoughts on how to solve this problem. \ncheck prime number at every step from 2 to n -> brute force\n\n# Approach\n Describe yo | kalamarham | NORMAL | 2022-12-25T07:05:32.648800+00:00 | 2022-12-25T07:05:32.648848+00:00 | 100 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\ncheck prime number at every step from 2 to n -> brute force\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\ncheck prime and find prime factors accordingly compxty will be reduced\n// Md Arham kalam Ansari\n# Complexity\n- Time complexity: O(sqrt(n)*sqrt(n))\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n bool check_prime(int n)\n {\n if(n<=1) return false;\n for(int i=2;i<=sqrt(n);i++)\n {\n if(n%i==0) return false;\n }\n return true;\n }\n void sol(int &n,vector<int> &v)\n {\n \n while(n%2==0)\n {\n v.push_back(2);\n n=n/2;\n }\n for(int i=3;i<=sqrt(n);i=i+2)\n {\n while(n%i==0)\n {\n v.push_back(i);\n n=n/i;\n }\n }\n if(n>2) v.push_back(n);\n }\n int smallestValue(int n) {\n vector<int> v;\n if(n==4) return n;\n if(check_prime(n)==true) return n;\n while(check_prime(n)==false)\n {\n sol(n,v);\n int ans=0;\n for(int i=0;i<v.size();i++) ans+=v[i];\n //accumulate(v.begin(),v.end(),ans);\n n=ans;\n v.clear();\n }\n return n;\n }\n};\n``` | 1 | 0 | ['C++'] | 1 |
smallest-value-after-replacing-with-sum-of-prime-factors | Simplest Recursion | simplest-recursion-by-vidhwanshak-bd95 | Intuition\n Describe your first thoughts on how to solve this problem. \nThe idea is very simple. We recursively call factorial function which calculates the su | VIDHWANSHAK | NORMAL | 2022-12-20T04:41:01.947111+00:00 | 2022-12-20T04:41:01.947144+00:00 | 318 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe idea is very simple. We recursively call `factorial` function which calculates the sum of factorial and pass it to the `smallestValue` function.\nThe only problem we will face is when sum of factorial be equal to the number `n` so we handle the case by using the `if` clause.\n\n\n# Code\n```\nclass Solution {\n int a = 2;\n public int smallestValue(int n) {\n int sum = factorial(n,2);\n // handle the anamoly\n if(sum == n)\n return n;\n return smallestValue(sum);\n }\n public static int factorial(int n , int a){\n if (n == 1) return 0;\n\n if(n%a == 0) return a + factorial(n/a, a);\n else return factorial(n, a+1);\n\n }\n}\n``` | 1 | 0 | ['Java'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | Exception handling solution java | exception-handling-solution-java-by-pron-j9wh | Intro\nProblem isn`t hard, so only one problem that i had is stackoverflow\n Describe your first thoughts on how to solve this problem. \n\n# Approach\nYeah, yo | pronnik | NORMAL | 2022-12-20T01:02:01.819900+00:00 | 2022-12-20T01:02:01.819943+00:00 | 193 | false | # Intro\nProblem isn`t hard, so only one problem that i had is stackoverflow\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nYeah, you can make it faster by making 1 more check. But stackOverflow helped me alot in my life so i decided to leave it here^_^ss\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: 959 ms\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: 466.4 MB\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n public static int smallestValue(int n) {\n try {\n int sum=0;\n int prev = n;\n for (int i = 2; i <= Math.sqrt(n); i++) {\n if (n%i==0){sum+=i; n/=i;i--;}\n }\n if (sum==0) return n;\n return smallestValue(sum+n);\n } catch (StackOverflowError a) {return n*2;}\n }\n}}\n}\n``` | 1 | 0 | ['Java'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | Simple Recursive approach | simple-recursive-approach-by-varunkusabi-xq99 | Approach\nWe will calculate the sum of all possible prime factors and compare it with the number of which we are calculating for if sum is less we will recursiv | varunkusabi8 | NORMAL | 2022-12-18T21:02:12.904944+00:00 | 2023-03-26T10:52:30.821200+00:00 | 38 | false | # Approach\nWe will calculate the sum of all possible prime factors and compare it with the number of which we are calculating for if sum is less we will recursively calculate for it or else return it since it breaks condition;\n# Complexity\n- Time complexity:\n- It is recursive in approach\n\n# Code\n```\nclass Solution {\npublic:\n int smallestValue(int n)\n {\n int q=n;\n int p=0;\n while(n%2==0)\n {\n p=p+2;\n n=n/2;\n }\n for(int i=3;i<=sqrt(n);i=i+2)\n {\n while(n%i==0)\n {\n p=p+i;\n n=n/i;\n }\n }\n if (n > 2)\n {\n p=p+n;\n }\n if(q>p)\n {\n q=p;\n return smallestValue(p);\n }\n else\n {\n return p;\n }\n }\n};\n\nPlease upvote if it helps!! | 1 | 0 | ['Recursion', 'C++'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | Clean Code || C++ || O(1) Space | clean-code-c-o1-space-by-arandomguy-74wl | ```\nclass Solution {\npublic:\n \n int smallestValue(int n) {\n int x = n;\n int ans = n;\n \n while(true) {\n in | ARandomGuy | NORMAL | 2022-12-18T19:29:26.028821+00:00 | 2022-12-18T19:33:54.844508+00:00 | 36 | false | ```\nclass Solution {\npublic:\n \n int smallestValue(int n) {\n int x = n;\n int ans = n;\n \n while(true) {\n int check = x; // Storing x in another variable as x will be lost in each iteration\n \n \n int num = x; // Condition variable to be used in for loop\n \n x = 0; // Reusing variable to store sum of factors\n \n \n // Sieve Algorithm to get prime factorisation\n \n for(int i = 2; i <= num; i++) {\n while(num % i == 0) {\n num = num / i;\n x += i; // Adding factor to x;\n }\n }\n \n ans = min(x, ans);\n \n if(x == check) break; // Base condition to break out i.e Sum of factors = Product of factors\n }\n \n return ans;\n }\n}; | 1 | 0 | ['C'] | 1 |
smallest-value-after-replacing-with-sum-of-prime-factors | C++ simple solution by help of AI | c-simple-solution-by-help-of-ai-by-obscu-t571 | I gave my solution to ChatGPT and it returned me this solution....\uD83D\uDE0D\uD83D\uDE0D\uD83D\uDE0D\nAlthough it\'s run-time is costly but managed to pass al | obscure_ | NORMAL | 2022-12-18T17:48:46.156095+00:00 | 2022-12-18T17:48:46.156146+00:00 | 300 | false | I gave my solution to ChatGPT and it returned me this solution....\uD83D\uDE0D\uD83D\uDE0D\uD83D\uDE0D\nAlthough it\'s run-time is costly but managed to pass all the test cases.\n\nclass Solution {\npublic:\n const int MAXN = 100005;\n\n int getPrimeFactorSum(int x) {\n // Implement a function for computing the sum of the prime factors of x here\n // For example, using trial division:\n int sum = 0;\n for (int i = 2; i <= x / i; i++) {\n while (x % i == 0) {\n sum += i;\n x /= i;\n }\n }\n if (x > 1) {\n sum += x;\n }\n return sum;\n }\n \n int getFactorization(int x){\n return getPrimeFactorSum(x);\n }\n \n int smallestValue(int n) {\n vector<int> pre(n + 1, 0);\n for(int i = 2; i <= n; i++){\n pre[i] = getFactorization(i);\n }\n \n int ans = INT_MAX;\n map<int, int> mp;\n while(n > 1){\n ans = min(ans, pre[n]);\n n = pre[n];\n if(mp.count(n))\n break;\n mp[n]++;\n }\n \n return ans;\n }\n};\n | 1 | 0 | ['C'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | Simple and Efficient C++ Solution | simple-and-efficient-c-solution-by-arko-xxqcq | \ntypedef long long ll;\nclass Solution {\npublic:\n ll f(int n)\n {\n ll s=0;\n ll y=n;\n for(int i=2;i<y;i++)\n {\n | Arko-816 | NORMAL | 2022-12-18T13:19:23.705514+00:00 | 2022-12-18T13:19:23.705815+00:00 | 200 | false | ```\ntypedef long long ll;\nclass Solution {\npublic:\n ll f(int n)\n {\n ll s=0;\n ll y=n;\n for(int i=2;i<y;i++)\n {\n if(isprime(i))\n {\n while(n%i==0)\n {\n n/=i;\n s+=i;\n if(isprime(n))\n {\n s+=n;\n return s;\n }\n }\n }\n \n }\n return s;\n }\n bool isprime(int n)\n {\n for(int i=2;i<n;i++)\n {\n if(n%i==0)\n return false;\n }\n return true;\n }\n int smallestValue(int n) {\n ll mini=n;\n ll s=n;\n while(!isprime(n))\n {\n ll x=f(n);\n n=x;\n mini=min(mini,x);\n if(s==x)\n return mini;\n s=x;\n \n }\n return mini;\n }\n};\n``` | 1 | 0 | [] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | My Submission || JAVA | my-submission-java-by-pappuraj-815q | \n\n# Code\n\nclass Solution {\n \n \n int cal(int num){\n int out=0;\n for(int i = 2; i< num; i++) {\n while(num%i == 0) {\n | PAPPURAJ | NORMAL | 2022-12-18T11:03:32.626585+00:00 | 2022-12-18T11:03:32.626621+00:00 | 39 | false | \n\n# Code\n```\nclass Solution {\n \n \n int cal(int num){\n int out=0;\n for(int i = 2; i< num; i++) {\n while(num%i == 0) {\n out+=i;\n num = num/i;\n }\n }\n if(num >2) {\n out+=num;\n }\n return out;\n }\n \n public int smallestValue(int n) {\n while(n!=cal(n)){\n \n if(n<4)\n return n; \n n=cal(n);\n }\n \n return n;\n }\n}\n``` | 1 | 0 | ['Java'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | SIMPLE PYTHON SOLUTION | simple-python-solution-by-beneath_ocean-yb2k | Intuition\nMy intuition is that if I encounter any prime number in the process then that is the minimum of till now will be the answer or if we come across same | beneath_ocean | NORMAL | 2022-12-18T10:51:52.293547+00:00 | 2022-12-18T10:51:52.293587+00:00 | 442 | false | # Intuition\nMy intuition is that if I encounter any prime number in the process then that is the minimum of till now will be the answer or if we come across same number twice then we will return the minimum value.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nfrom math import sqrt\n\nclass Solution:\n def primeFactors(self,n):\n x=0\n while n % 2 == 0:\n x+=2\n n = n // 2\n\n for i in range(3,int(math.sqrt(n))+1,2):\n while n % i== 0:\n x+=i\n n = n // i\n if n > 2:\n x+=n\n return x\n\n def isPrime(self,n):\n prime_flag = 0\n if(n > 1):\n for i in range(2, int(sqrt(n)) + 1):\n if (n % i == 0):\n prime_flag = 1\n break\n if (prime_flag == 0):\n return True\n else:\n return False\n else:\n return True\n\n def smallestValue(self, n: int) -> int:\n lst=[0]*100001\n mn=n\n \n while True:\n if lst[n]==1:\n return mn\n if self.isPrime(n):\n return min(n,mn)\n lst[n]=1\n n=self.primeFactors(n)\n mn=min(mn,n)\n\n \n``` | 1 | 0 | ['Python3'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | Easy python solution | easy-python-solution-by-harshgajera28-etxm | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | HarshGajera28 | NORMAL | 2022-12-18T09:39:52.226339+00:00 | 2022-12-18T09:39:52.226380+00:00 | 328 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n ---> 61 ms\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n \n# Code\n```\nclass Solution:\n def smallestValue(self, n: int) -> int:\n def pr(n):\n x=[]\n while n % 2 == 0:\n x.append(2)\n n = n / 2\n for i in range(3,int(math.sqrt(n))+1,2):\n while n % i== 0:\n x.append(i)\n n = n / i\n if n > 2:\n x.append(n)\n return sum(x)\n while(n!=pr(n)):\n n=pr(n)\n return int(n) \n \n``` | 1 | 0 | ['Python3'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | Easy C++ Code | easy-c-code-by-aishwaryadz-zydz | Code\n\nclass Solution {\npublic:\n bool isprime(int n){\n if (n <= 1)return false;\n for (int i = 2; i < n; i++)\n if (n % i == 0)return false;\ | aishwaryadz | NORMAL | 2022-12-18T06:33:34.524765+00:00 | 2022-12-18T06:33:34.524825+00:00 | 477 | false | # Code\n```\nclass Solution {\npublic:\n bool isprime(int n){\n if (n <= 1)return false;\n for (int i = 2; i < n; i++)\n if (n % i == 0)return false;\n return true;\n}\n void primeFactors(int n,vector<int>&v){\n while (n % 2 == 0){\n v.push_back(2);\n n = n/2;\n }\n for (int i = 3; i <= sqrt(n); i = i + 2){\n while (n % i == 0)\n {\n v.push_back(i);\n n = n/i;\n }\n }\n if (n > 2)\n v.push_back(n);\n}\n int smallestValue(int n) {\n if(n==0||n==1||n==4)return n;\n int sum;\n while(!isprime(n)){\n sum=0;\n vector<int>v;\n primeFactors(n,v);\n for(int i=0;i<v.size();i++)sum+=v[i];\n n=sum;\n }\n return sum;\n }\n};\n``` | 1 | 0 | ['C++'] | 2 |
smallest-value-after-replacing-with-sum-of-prime-factors | CodeDominar Solution | codedominar-solution-by-codedominar-6qp0 | Code\n\nclass Solution:\n def smallestValue(self, n: int) -> int:\n def find_factors_sum(n):\n sum_ = 0\n for i in range(2,n):\n | codeDominar | NORMAL | 2022-12-18T06:26:30.310094+00:00 | 2022-12-18T06:26:30.310135+00:00 | 61 | false | # Code\n```\nclass Solution:\n def smallestValue(self, n: int) -> int:\n def find_factors_sum(n):\n sum_ = 0\n for i in range(2,n):\n while n%i==0:\n sum_+=i\n n = n//i\n return sum_\n while find_factors_sum(n) and n!=find_factors_sum(n):\n n = find_factors_sum(n)\n return n\n \n \n \n``` | 1 | 0 | ['Python3'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | ✅ Java || Sieve of Eratosthenes Approach || Highly optimised code | java-sieve-of-eratosthenes-approach-high-2pli | Intuition:\nKeep breaking the number into sum of prime factors. Stop when the number becomes a prime number. Because prime numbers cannot be broken further.\n\n | Suvradippaul | NORMAL | 2022-12-18T05:09:12.520052+00:00 | 2022-12-18T14:05:50.674040+00:00 | 303 | false | **Intuition:**\n*Keep breaking the number into sum of prime factors. Stop when the number becomes a prime number. Because prime numbers cannot be broken further.*\n\n```java\nclass Solution {\n public int smallestValue(int n) {\n fillPrimes(n); // finding all primes upto n by sieve of eratosthenes\n \n int prev = n;\n while (!isPrime[n]) {\n n = sumOfPrimeFactors(n);\n if (prev == n) break;\n prev = n;\n }\n \n return n;\n }\n \n int sumOfPrimeFactors(int n) {\n int sum = 0;\n int i = 2;\n while (i <= n) {\n if (isPrime[i] && n%i == 0) {\n n /= i;\n sum += i;\n }\n else {\n i++;\n }\n }\n return sum;\n }\n\t\n\tboolean[] isPrime;\n\tboolean[] vis;\n\tvoid fill(int n) {\n\t\tisPrime = new boolean[n+1];\n\t\tArrays.fill(isPrime, true);\n\t\tvis = new boolean[n+1];\n\t\t\n\t\tfor (int i = 2; i <= n/i; i++) {\n\t\t\tif (!vis[i]) {\n\t\t\t\tint num = i;\n\t\t\t\tint mul = 2;\n\t\t\t\twhile (num * mul < isPrime.length) {\n\t\t\t\t\tint prod = num*mul;\n\t\t\t\t\tisPrime[prod] = false;\n\t\t\t\t\tvis[prod] = true;\n\t\t\t\t\tmul++;\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n}\n``` | 1 | 0 | ['Java'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | c++ beginer friendly || easy solution||simplified | c-beginer-friendly-easy-solutionsimplifi-ayzi | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | madhur013 | NORMAL | 2022-12-18T05:02:16.312329+00:00 | 2022-12-18T05:08:39.872668+00:00 | 137 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n int smallestValue(int n) {\n int num=n;\n n=prime(n);\n \n while(num!=n)\n {\n num=n; \n n=prime(n);\n \n }\n return num;\n } \n int prime(int n)\n {\n int a=n;\n int ans =0;\n for(int i=2;i<a;++i)\n {\n while(n%i==0)\n {\n ans+=i;\n n/=i;\n }\n }\n if(ans==0)\n return a;\n else return ans ;\n}\n};\n\n``` | 1 | 0 | ['Math', 'Recursion', 'C++'] | 1 |
smallest-value-after-replacing-with-sum-of-prime-factors | C++, Fully explained 5 line code | c-fully-explained-5-line-code-by-freakin-q75x | Intuition\nFirst thought is to solve with the help of bool prime and so on, but recursively we don\'t need to find the prime. \n# Approach\n Describe your appro | freakinrkb | NORMAL | 2022-12-18T04:34:20.519808+00:00 | 2022-12-18T04:34:20.519841+00:00 | 34 | false | # Intuition\nFirst thought is to solve with the help of bool prime and so on, but recursively we don\'t need to find the prime. \n# Approach\n<!-- Describe your approach to solving the problem. -->\nFirst calculate all the prime factor of the number, then sum all of it.\nThen u recursively pass the sum to our function, and check if the sum is equal to previously existing number.\nFor ex :\nWe take 15 -\nfactors are - 3 ,5,15\nprime no are - 3,5\nsum is 8.\n\nnow we have to find out the prime factor of 8.\nwhich is 2, 2 ,2.\nsum is 6.\n\nagain we pass 6 as we know 8 != 6.\nprime factor of 6 = 2,3\nsum is 5.\n\nagain we pass 5, because 5 != 6.\n5 is prime number. prime factor = 5 only.\n\nwe pass 5 again which stisfy the condition, 5==5\nHence base case satisfied we return 5 as the answer.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\no(n)\n\n# Code\n```\nclass Solution {\npublic:\nint check(int n){\n int sum = 0;\n int c = 2; \n while(n > 1){\n if(n % c == 0){\n sum += c;\n n /= c;\n }\n else{\n c++;\n }\n }\n return sum;\n}\n\n int smallestValue(int n) {\n int ans=check(n);\n if(ans==n) return ans;\n else return smallestValue(ans);\n }\n};\n``` | 1 | 0 | ['C++'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | JavaScript easy | javascript-easy-by-gamboadd27-db3g | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | GamboaDd27 | NORMAL | 2022-12-18T04:25:17.723452+00:00 | 2022-12-18T04:25:17.723492+00:00 | 145 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\n/**\n * @param {number} n\n * @return {number}\n */\nvar smallestValue = function(n) {\n let s=(primeFactors(n))\n if(s==n){\n return s\n }\n while(true){\n let nn=primeFactors(n)\n if(nn==n){\n return n\n }\n n=nn\n }\n \n return 0\n \n \n};\n\nfunction primeFactors(n)\n{\n let ans=0\n let c = 2;\n while(n > 1)\n {\n if(n % c == 0){\n ans+=c\n n /= c;\n }\n else c++;\n }\n return ans\n}\n``` | 1 | 0 | ['JavaScript'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | JAVA | Simplest and Shortest ✅ | java-simplest-and-shortest-by-sourin_bru-tj2s | Please Upvote :D\njava []\nclass Solution {\n public int smallestValue(int n) {\n int ori = n, ans = n;\n int k = 2, sum = 0;\n\n while | sourin_bruh | NORMAL | 2022-12-18T04:19:59.284884+00:00 | 2022-12-18T04:19:59.284927+00:00 | 79 | false | # Please Upvote :D\n``` java []\nclass Solution {\n public int smallestValue(int n) {\n int ori = n, ans = n;\n int k = 2, sum = 0;\n\n while (true) {\n if (n % k == 0) {\n sum += k;\n n /= k;\n }\n else k++;\n\n if (n == 1) {\n if (sum == ori) {\n break;\n }\n ori = n = sum;\n sum = 0;\n k = 2;\n ans = Math.min(ans, n);\n }\n }\n\n return ans;\n }\n}\n``` | 1 | 0 | ['Java'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | simple C++ approach | simple-c-approach-by-sudhanwaofficial-4mr7 | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | sudhanwaofficial | NORMAL | 2022-12-18T04:18:56.681385+00:00 | 2022-12-18T04:18:56.681427+00:00 | 316 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n \n bool prime(int n){\n if (n <= 1)\n return false;\n \n \n for (int i = 2; i < n; i++)\n if (n % i == 0)\n return false;\n \n return true;\n \n }\n \n int cal(int n){\n \n vector<int> vec;\n \n for(int i=2;i<n;i++){\n // cout<<i;\n if(prime(i) && n%i==0){\n n=n/i;\n vec.push_back(i);\n i=1;\n if(prime(n)) vec.push_back(n);\n }\n // cout<<n;\n \n }\n \n \n int sum=0;\n \n for(auto i: vec) sum+=i;\n vec.clear();\n return sum;\n }\n \n int smallestValue(int n) {\n \n cout<<"hi";\n \n while(!prime(n)){\n if(n==4) return 4;\n n=cal(n);\n \n }\n \n return n;\n }\n};\n``` | 1 | 0 | ['C++'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | Simplest Easiest Solution C++ | simplest-easiest-solution-c-by-vivekkuma-vtq4 | \n int smallestValue(int n)\n {\n\n int sum = 0;\n bool flag = false;\n int i = 2;\n while (i <= n / 2)\n {\n | Vivekkumarmishra | NORMAL | 2022-12-18T04:16:41.435729+00:00 | 2022-12-18T04:16:41.435758+00:00 | 42 | false | \n int smallestValue(int n)\n {\n\n int sum = 0;\n bool flag = false;\n int i = 2;\n while (i <= n / 2)\n {\n if (n % i == 0)\n {\n sum += i;\n flag = true;\n n /= i;\n continue;\n }\n i++;\n }\n if (n != 1 && flag == 1)\n {\n sum += n;\n }\n if (n == 2 && sum == 4)\n {\n return sum;\n }\n if (flag == 0)\n {\n return n;\n }\n return smallestValue(sum);\n }\n | 1 | 0 | ['Math', 'Recursion', 'C'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | Java | Easy to understand solution + Comments | 5 ms solution | java-easy-to-understand-solution-comment-jqk7 | \nclass Solution {\n public int smallestValue(int n) {\n while(true){\n int sum = sumOfPrimeFactors(n);\n if(sum == n) break; // | theGDM | NORMAL | 2022-12-18T04:14:17.827543+00:00 | 2022-12-18T04:15:53.304761+00:00 | 194 | false | ```\nclass Solution {\n public int smallestValue(int n) {\n while(true){\n int sum = sumOfPrimeFactors(n);\n if(sum == n) break; //if sum and n both becomes equal than break the loop\n n = sum; //update n with sum of its prime factors\n }\n \n return n;\n }\n \n public int sumOfPrimeFactors(int num){\n int factor = 2;\n int sum = 0;\n\n while(num != 1){ //Repeat the loop till number becomes 1.\n if(num % factor == 0){ //Check if factor divides the number.\n num /= factor; //If yes, update the number.\n sum += factor; //Add factor to sum.\n continue;\n } \n \n factor++; //If the current number is not a factor, check the next number.\n }\n return sum;\n } \n} \n\n\n``` | 1 | 0 | ['Java'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | Easy C++ solution | easy-c-solution-by-dheeruthakur-1lm3 | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | DheeruThakur | NORMAL | 2022-12-18T04:10:41.850269+00:00 | 2022-12-18T04:10:41.850310+00:00 | 261 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n bool isPrime(int n){\n if(n<2) return false;\n for(int i=2 ; i<=sqrt(n) ; i++){\n if(n%i == 0){\n return false;\n }\n }\n return true;\n }\n int smallestValue(int n) {\n if(isPrime(n)) return n;\n int mini = INT_MAX;\n int m = n;\n int sum;\n do\n { \n sum = 0;\n while(n%2==0){\n sum += 2;\n n=n/2;\n }\n for(int i=3 ; i<=sqrt(n) ; i=i+2){\n while(n%i == 0){\n sum += i;\n n = n/i;\n }\n }\n if(n>2){\n sum += n;\n }\n mini = min(mini , sum);\n n = sum;\n }while(isPrime(sum) == false && sum < m);\n \n return mini;\n }\n};\n``` | 1 | 0 | ['C++'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | [C++] Beats 92% || Prime Factorization | c-beats-92-prime-factorization-by-amanra-q693 | Code\n\nclass Solution {\npublic:\n int primeFactors(int n)\n {\n int sum = 0;\n\xA0\xA0\xA0\xA0 int count = 2;\n\xA0\xA0\xA0\xA0 while(n>1)\ | amanrathodpro | NORMAL | 2022-12-18T04:09:27.946888+00:00 | 2022-12-18T04:09:27.946921+00:00 | 54 | false | # Code\n```\nclass Solution {\npublic:\n int primeFactors(int n)\n {\n int sum = 0;\n\xA0\xA0\xA0\xA0 int count = 2;\n\xA0\xA0\xA0\xA0 while(n>1)\n\xA0\xA0\xA0 \xA0 {\n\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0 if(n % count == 0){\n\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0 sum += count;\n\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0 n/=count;\n\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0 }\n\xA0\xA0\xA0\xA0\xA0\xA0\xA0\xA0 else count++;\n\xA0\xA0\xA0\xA0 }\n \n return sum;\n }\n \n int smallestValue(int num) {\n int sum = 0;\n int prev = 0;\n \n map<int,int> visited;\n\xA0 \n while (num) {\n prev = num;\n\xA0\xA0\xA0\xA0 num = primeFactors(num);\n \n // if current prime factor is visited again then return the previous factor\n if (visited[num]) {\n return prev;\n } else {\n visited[num] = 1;\n }\n }\n \n\xA0\xA0\xA0\xA0 return 1;\n }\n};\n``` | 1 | 0 | ['C++'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | Python | python-by-diwakar_4-iarv | Complexity\n- Time complexity: This Approach takes O(log n) for all composite numbers and O(n) otherwise.\n- Space complexity: O(n)\n\n# Code\n\nclass Solution: | diwakar_4 | NORMAL | 2022-12-18T04:06:08.302336+00:00 | 2022-12-18T04:06:08.302372+00:00 | 101 | false | # Complexity\n- Time complexity: This Approach takes O(log n) for all composite numbers and O(n) otherwise.\n- Space complexity: O(n)\n\n# Code\n```\nclass Solution:\n def smallestValue(self, n: int) -> int:\n def prime(n):\n prev_n = n\n s = 0\n c = 2\n while(n > 1):\n if(n % c == 0):\n s += c\n n = n / c\n else:\n c = c + 1\n if s == prev_n:\n return False\n else:\n return s\n \n ans = n\n while 1:\n res = prime(ans)\n if res:\n ans = res\n else:\n break\n \n return ans\n```\n\n----------------\n**Upvote the post if you find it helpful.\nHappy coding.** | 1 | 0 | ['Python3'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | Java || Brute force | java-brute-force-by-henryzhc-wa0s | \nclass Solution {\n public int smallestValue(int n) {\n int check = n;\n while (isPrime(check) != 1) {\n int curr = getValue(check) | henryzhc | NORMAL | 2022-12-18T04:05:23.953417+00:00 | 2022-12-18T04:05:23.953460+00:00 | 73 | false | ```\nclass Solution {\n public int smallestValue(int n) {\n int check = n;\n while (isPrime(check) != 1) {\n int curr = getValue(check);\n if (curr == check) {\n return check;\n }\n check = curr;\n }\n return check;\n }\n public int getValue(int n) {\n int res = 0;\n int check = n;\n for (int i = 2; i <= n; i++) {\n if (isPrime(i) == 1) {\n while (check % i == 0) {\n res += i;\n check /= i;\n }\n }\n }\n return res;\n }\n public int isPrime(int n) {\n for (int i = 2; i <= Math.sqrt(n); i++) {\n while (n % i == 0) {\n return i;\n }\n }\n return 1;\n }\n}\n``` | 1 | 0 | ['Java'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | Easy Java Solution | easy-java-solution-by-sgsumitgupta17-6r6f | \n# Code\n\nclass Solution {\n public int smallestValue(int n) {\n int sum = 0;\n int ans=Integer.MAX_VALUE;\n HashSet<Integer> hs=new H | sgsumitgupta17 | NORMAL | 2022-12-18T04:02:36.879460+00:00 | 2022-12-18T04:50:38.398590+00:00 | 190 | false | \n# Code\n```\nclass Solution {\n public int smallestValue(int n) {\n int sum = 0;\n int ans=Integer.MAX_VALUE;\n HashSet<Integer> hs=new HashSet<>();\n while(!hs.contains(n)){\n hs.add(n);\n // check from 2 to n whenever n is the factor of c add it to the sum\n int c = 2;\n while (n > 1) {\n if (n % c == 0) {\n sum+=c;\n n /= c;\n }\n else\n c++;\n }\n ans=Math.min(sum,ans);\n n=sum;\n sum=0;\n }\n return ans;\n }\n \n}\n``` | 1 | 0 | ['Java'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | Easy Understandable Solution [c++] | easy-understandable-solution-c-by-algoar-bi6a | Code\n\nclass Solution {\npublic:\n int smallestValue(int n) {\n int k=2;\n int sum=0;\n int maxi=n;\n int mini=n;\n while | AlgoArtisan | NORMAL | 2022-12-18T04:01:44.064555+00:00 | 2022-12-18T04:05:57.230939+00:00 | 434 | false | # Code\n```\nclass Solution {\npublic:\n int smallestValue(int n) {\n int k=2;\n int sum=0;\n int maxi=n;\n int mini=n;\n while(true){\n if(n%k==0){\n sum+=k;\n n=n/k;\n }\n else k++;\n if(n==1){\n if(sum==maxi) break;\n n=sum;\n sum=0;\n mini=min(n,mini);\n k=2;\n maxi=n;\n }\n \n }\n return mini;\n }\n};\n``` | 1 | 0 | ['C++'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | [Java] Brute force with math | java-brute-force-with-math-by-0x4c0de-mx5d | Intuition\n Describe your first thoughts on how to solve this problem. \nDecrease n with brute force.\n\n# Approach\n Describe your approach to solving the prob | 0x4c0de | NORMAL | 2022-12-18T04:01:12.263363+00:00 | 2022-12-18T04:59:47.290176+00:00 | 456 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nDecrease n with brute force.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nWatch out the $$4 = 2 * 2$$, decrease n with prime sum.\n\nFor some one want to find the math: [Fundamental theorem of arithmetic](https://en.wikipedia.org/wiki/Fundamental_theorem_of_arithmetic)\n\n# Complexity\n- Time complexity: $$O(N)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n public int smallestValue(int n) {\n // watch out 4 = 2 * 2\n if (n == 4) {\n return 4;\n }\n\n while (!prime(n)) {\n int total = 0;\n for (int i = 2; i <= n; i++) {\n if (prime(i)) {\n while (n % i == 0) {\n total += i;\n n /= i;\n }\n }\n }\n n = total;\n }\n return n;\n }\n\n private boolean prime(int n) {\n if (n < 4) {\n return true;\n }\n for (int i = 2; i * i <= n; i++) {\n if (n % i == 0) {\n return false;\n }\n }\n return true;\n }\n}\n\n``` | 1 | 0 | ['Math', 'Java'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | Easy Recursive Soln. | easy-recursive-soln-by-realyogendra-n8ka | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | realyogendra | NORMAL | 2025-04-07T16:53:32.446291+00:00 | 2025-04-07T16:53:32.446291+00:00 | 1 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```cpp []
class Solution {
public:
int smallestValue(int n) {
int initialVal = n;
int sumPrimeFactors = 0;
for(int div = 2; div * div <= n; div++) {
while( n%div == 0) {
sumPrimeFactors += div;
n = n / div;
}
}
if( n > 1) {
sumPrimeFactors += n;
}
if(sumPrimeFactors != initialVal){
return smallestValue(sumPrimeFactors);
}
return sumPrimeFactors;
}
};
``` | 0 | 0 | ['C++'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | C++ || Simple Prime Factorization | c-simple-prime-factorization-by-sawarn24-ldil | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | sawarn2411 | NORMAL | 2025-04-06T02:21:58.461532+00:00 | 2025-04-06T02:21:58.461532+00:00 | 2 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```cpp []
class Solution {
public:
int smallestValue(int n) {
int s=0;
while(s!=n)
{
s=n;
n=sumOfPrimeFactors(n);
}
return s;
}
int sumOfPrimeFactors(int n) {
int sum = 0;
while (n % 2 == 0) {
sum += 2;
n /= 2;
}
for (int i = 3; i * i <= n; i += 2) {
while (n % i == 0) {
sum += i;
n /= i;
}
}
if (n > 2) {
sum += n;
}
return sum;
}
};
``` | 0 | 0 | ['C++'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | Floyd's fast and slow approach in Java | floyds-fast-and-slow-approach-in-java-by-g1vz | IntuitionI saw that the least number is always reached in every case.ApproachI used prime factorisation and Floyd's Two pointer approach to efficiently calculat | 22blc1217 | NORMAL | 2025-03-23T15:17:57.208408+00:00 | 2025-03-23T15:17:57.208408+00:00 | 2 | false | # Intuition
I saw that the least number is always reached in every case.
# Approach
I used prime factorisation and Floyd's Two pointer approach to efficiently calculate the stopping point.
# Complexity
- Time complexity:O(sqrt(n)*log(n)) because it takes log(n) time to calculate prime factors and sqrt(n) time to iterate upto square root of n.
- Space complexity:
O(1) as no extra space is used
# Code
```java []
class Solution {
public static int calculateSumOfPrimes(int n) {
int sum = 0;
// Extract all 2s
while (n % 2 == 0) {
sum += 2;
n /= 2;
}
// Check for odd prime factors
for (int i = 3; i * i <= n; i += 2) {
while (n % i == 0) {
sum += i;
n /= i;
}
}
// If n is still greater than 2, it's a prime itself
if (n > 2) {
sum += n;
}
return sum;
}
public static int smallestValue(int n) {
int slow = n;
int fast = calculateSumOfPrimes(n);
// Use Floyd's cycle detection method
while (slow != fast) {
slow = calculateSumOfPrimes(slow);
fast = calculateSumOfPrimes(calculateSumOfPrimes(fast));
}
return slow;
}
}
``` | 0 | 0 | ['Java'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | Easy solution | easy-solution-by-koushik_55_koushik-gtmx | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | Koushik_55_Koushik | NORMAL | 2025-03-21T15:33:51.568406+00:00 | 2025-03-21T15:33:51.568406+00:00 | 3 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```python3 []
class Solution:
def smallestValue(self, n: int) -> int:
def fre(n):
l=[]
while n%2==0:
l.append(2)
n=n//2
for i in range(3,int(n**0.5)+1,2):
while n%i==0:
l.append(i)
n=n//i
if n>2:
l.append(n)
k=sum(l)
return k
while True:
f=fre(n)
if f==n:
break
n=f
return n
``` | 0 | 0 | ['Python3'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | Best c++ solution Beats 100%. | best-c-solution-beats-100-by-harsh21ngh-4002 | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | harsh21ngh | NORMAL | 2025-03-15T13:13:38.025003+00:00 | 2025-03-15T13:13:38.025003+00:00 | 1 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```cpp []
class Solution {
public:
bool isPrime(int n){
if(n==1) return false;
for(int i=2;i<=sqrt(n);i++){
if(n%i==0) return false;
}
return true;
}
int smallestValue(int n) {
if(isPrime(n)) return n;
int sum=0;
for(int i=1;i<sqrt(n);i++){
if(n%i==0 && isPrime(i)){
int m=n;
while(m%i==0){
sum+=i;
m/=i;
}
}
}
for(int i=sqrt(n);i>=1;i--){
if(n%i==0 && isPrime(n/i)){
int m=n;
while(m%(n/i)==0){
sum+=(n/i);
m/=(n/i);
}
}
}
if(sum==4) return sum;
return (smallestValue(sum));
}
};
``` | 0 | 0 | ['C++'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | Best c++ solution Beats 100%. | best-c-solution-beats-100-by-harsh21ngh-fqd7 | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | harsh21ngh | NORMAL | 2025-03-15T13:13:35.540600+00:00 | 2025-03-15T13:13:35.540600+00:00 | 3 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```cpp []
class Solution {
public:
bool isPrime(int n){
if(n==1) return false;
for(int i=2;i<=sqrt(n);i++){
if(n%i==0) return false;
}
return true;
}
int smallestValue(int n) {
if(isPrime(n)) return n;
int sum=0;
for(int i=1;i<sqrt(n);i++){
if(n%i==0 && isPrime(i)){
int m=n;
while(m%i==0){
sum+=i;
m/=i;
}
}
}
for(int i=sqrt(n);i>=1;i--){
if(n%i==0 && isPrime(n/i)){
int m=n;
while(m%(n/i)==0){
sum+=(n/i);
m/=(n/i);
}
}
}
if(sum==4) return sum;
return (smallestValue(sum));
}
};
``` | 0 | 0 | ['C++'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | C++ easy solution 🏆😎💥🥇 | c-easy-solution-by-rishikanaujiya9598-tpvv | Code | rishikanaujiya9598 | NORMAL | 2025-03-11T06:14:38.566517+00:00 | 2025-03-11T06:14:38.566517+00:00 | 2 | false |
# Code
```cpp []
class Solution {
public:
// Function to compute the sum of prime factors of n
int factorSum(int n) {
int ans = 0;
int divisor = 2;
while (n > 1) {
if (n % divisor == 0) {
ans += divisor;
n /= divisor;
} else {
divisor++;
}
}
return ans;
}
int smallestValue(int n) {
while (true) {
int sum = factorSum(n);
if (n == sum) break;
n = sum;
}
return n;
}
};
``` | 0 | 0 | ['C++'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | C++ || Easy way to find smallest value | c-easy-way-to-find-smallest-value-by-cem-4qr9 | IntuitionWe need to find the smallest value, and this smallest value will always be a prime number.ApproachIn a "while" loop, we try to find the divisors of our | cemgate | NORMAL | 2025-03-08T21:13:31.692774+00:00 | 2025-03-08T21:13:31.692774+00:00 | 6 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
We need to find the smallest value, and this smallest value will always be a prime number.
# Approach
<!-- Describe your approach to solving the problem. -->
In a "while" loop, we try to find the divisors of our number and add them to primeSum. If primeSum is equal to our number, it means we have found the smallest number, and we return it. Otherwise, we continue searching for the smallest number.
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```cpp []
class Solution {
public:
int smallestValue(int n)
{
int primeSum=0;
int tmpN=n;
while(tmpN!=1)
{
for(int i =2 ; i <=tmpN;++i)
{
if(tmpN%i==0)
{
tmpN=tmpN/i;
primeSum+=i;
break;
}
}
}
//if primeSum == n that means its prime number so its the smallest value
return primeSum==n ? n : smallestValue(primeSum);
}
};
``` | 0 | 0 | ['C++'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | Simple C++ Solution 🤯🤯🤯 | simple-c-solution-by-harsh_suthar-uai2 | 🧠 IntuitionWe start by making a function that checks whether a number is divisible by a divisor. If it is, we add the divisor to the sum (as it's a factor) and | Harsh_Suthar | NORMAL | 2025-03-07T09:25:56.028828+00:00 | 2025-03-07T09:25:56.028828+00:00 | 25 | false | # 🧠 Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
We start by making a function that checks whether a number is divisible by a divisor. If it is, we add the divisor to the sum (as it's a factor) and divide the number by the same divisor. 🔢
We continue this process until the number becomes `1`. Inside the main function, we repeatedly call this factor sum function. If our input integer is equal to the sum of its factors, we stop 🛑; otherwise, we replace the integer with the computed sum and continue.
# 🛠️ Approach
<!-- Describe your approach to solving the problem. -->
1️⃣ Define a helper function `factorSum(n)` that finds the sum of the prime factors of `n`.
2️⃣ In `smallestValue(n)`, repeatedly call `factorSum(n)` until `n` stabilizes (i.e., does not change between iterations).
3️⃣ Return the final stable value. ✅
# 🔍 Step-by-Step Execution
### **Example: n = 20**
Let's break down how the loops work step by step:
#### **Factorization Process (factorSum function)**
| 🏁 Step | 🎯 `n` (Remaining Number) | ➗ `divisor` | 🔢 `ans` (Sum of Factors) |
|------|----------------|-----------|----------------|
| 1️⃣ | 20 | 2 | 2 |
| 2️⃣ | 10 | 2 | 4 |
| 3️⃣ | 5 | 5 | 9 |
| ✅ | 1 | - | **9** (Final) |
🔹 **Output of `factorSum(20)`**: **9**
Since `n ≠ sum`, we replace `n = 9` and repeat 🔄.
#### **Next Iteration (factorSum(9))**
| 🏁 Step | 🎯 `n` (Remaining Number) | ➗ `divisor` | 🔢 `ans` (Sum of Factors) |
|------|----------------|-----------|----------------|
| 1️⃣ | 9 | 3 | 3 |
| 2️⃣ | 3 | 3 | 6 |
| ✅ | 1 | - | **6** (Final) |
🔹 **Output of `factorSum(9)`**: **6**
Since `n ≠ sum`, we replace `n = 6` and repeat 🔄.
#### **Next Iteration (factorSum(6))**
| 🏁 Step | 🎯 `n` (Remaining Number) | ➗ `divisor` | 🔢 `ans` (Sum of Factors) |
|------|----------------|-----------|----------------|
| 1️⃣ | 6 | 2 | 2 |
| 2️⃣ | 3 | 3 | 5 |
| ✅ | 1 | - | **5** (Final) |
🔹 **Output of `factorSum(6)`**: **5**
Since `n ≠ sum`, we replace `n = 5` and repeat 🔄.
#### **Next Iteration (factorSum(5))**
| 🏁 Step | 🎯 `n` (Remaining Number) | ➗ `divisor` | 🔢 `ans` (Sum of Factors) |
|------|----------------|-----------|----------------|
| 1️⃣ | 5 | 5 | 5 |
| ✅ | 1 | - | **5** (Final) |
🔹 **Output of `factorSum(5)`**: **5**
Now, `n == sum`, so we stop 🛑 and return **5** as the answer 🎯.
# ⏳ Complexity Analysis
- **Time Complexity**:
- The worst-case scenario involves factorizing `n`, which takes at most **`O(log n)`**, and iterating this process **`O(log n)`** times.
- Thus, the overall time complexity is **`O((log n)^2)`**. ⚡
- **Space Complexity**:
- We use only a few integer variables, leading to a constant space usage of **`O(1)`**. 💾
# 🏆 Code
```cpp
class Solution {
public:
// Function to compute the sum of prime factors of n
int factorSum(int n) {
int ans = 0;
int divisor = 2;
while (n > 1) {
if (n % divisor == 0) {
ans += divisor;
n /= divisor;
} else {
divisor++;
}
}
return ans;
}
int smallestValue(int n) {
while (true) {
int sum = factorSum(n);
if (n == sum) break;
n = sum;
}
return n;
}
};
| 0 | 0 | ['C++'] | 1 |
smallest-value-after-replacing-with-sum-of-prime-factors | Math | Simple solution with 100% beaten | math-simple-solution-with-100-beaten-by-dqvo3 | IntuitionThe problem is about repeatedly replacing a number n with the sum of its prime factors until it becomes stable (meaning the sum of its prime factors eq | Takaaki_Morofushi | NORMAL | 2025-03-06T02:25:06.334552+00:00 | 2025-03-06T02:25:06.334552+00:00 | 5 | false | # Intuition
The problem is about repeatedly replacing a number `n` with the sum of its prime factors until it becomes stable (meaning the sum of its prime factors equals itself).
The intuition is that prime factorization helps us break the number into its smallest building blocks, and summing them gradually reduces the number or stabilizes it.
# Approach
- Loop until the number becomes stable (i.e., the sum of its prime factors equals the number itself).
- For each iteration:
- Factorize the number `n` by dividing it by all possible factors starting from 2.
- Sum up all prime factors.
- If the sum equals the original number, return the number (it's stable).
- Otherwise, set `n` to the sum and repeat.
# Complexity
- **Time complexity:**
$$O(\sqrt{n} \cdot \log n)$$
Each factorization takes $$O(\sqrt{n})$$ time, and since we repeat the process until stabilization, the total time is multiplied by the number of iterations, which is roughly $$O(\log n)$$ in the worst case.
- **Space complexity:**
$$O(1)$$
We use only a constant amount of extra space.
# Code
```cpp
class Solution {
public:
int smallestValue(int n) {
while (true) {
int sum = 0, x = n;
for (int i = 2; i * i <= n; ++i) {
while (n % i == 0) {
sum += i;
n /= i;
}
}
if (n > 1) sum += n;
if (sum == x) return x;
n = sum;
}
}
};
| 0 | 0 | ['C++'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | for beginners | for-beginners-by-abdulraoofkp-rbxl | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | abdulraoofkp | NORMAL | 2025-02-20T17:46:29.482950+00:00 | 2025-02-20T17:46:29.482950+00:00 | 4 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```javascript []
/**
* @param {number} n
* @return {number}
*/
var smallestValue = function(n) {
let primeFacsum=(n)=>{
let sum=0
while(n%2==0){
sum+=2
n=n/2
}
for(let i=3;i<=n;i+=2){
while(n%i==0){
sum+=i
n=n/i
}
}
return sum
}
while(n>primeFacsum(n)){
n=primeFacsum(n)
}
return n
};
``` | 0 | 0 | ['JavaScript'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | for beginners | for-beginners-by-abdulraoofkp-0sj7 | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | abdulraoofkp | NORMAL | 2025-02-20T17:46:26.235537+00:00 | 2025-02-20T17:46:26.235537+00:00 | 4 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```javascript []
/**
* @param {number} n
* @return {number}
*/
var smallestValue = function(n) {
let primeFacsum=(n)=>{
let sum=0
while(n%2==0){
sum+=2
n=n/2
}
for(let i=3;i<=n;i+=2){
while(n%i==0){
sum+=i
n=n/i
}
}
return sum
}
while(n>primeFacsum(n)){
n=primeFacsum(n)
}
return n
};
``` | 0 | 0 | ['JavaScript'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | ~20 | 20-by-lerfich-0yrb | IntuitionApproachComplexity
Time complexity:
O(log_p(sqrt(n))), p - most common factor except 2
Space complexity:
O(log2(n)) - n - max pow of 2, if all the f | lerfich | NORMAL | 2025-02-14T08:06:32.118941+00:00 | 2025-02-14T08:06:32.118941+00:00 | 3 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
O(log_p(sqrt(n))), p - most common factor except 2
- Space complexity:
O(log2(n)) - n - max pow of 2, if all the factors are '2'
# Code
```typescript []
function smallestValue(n: number): number {
let res = n
let min = 10
let prev = -1
while (true) {
prev = res
const arr = primeFactors(res)
res = arr.reduce((sum, item) => sum + item, 0)
if (arr.length === 1 && arr[0] === res || prev === res) {
break
}
}
return res
};
function primeFactors(n) {
const arr = []
// Print the number of 2s that divide n
while (n % 2 == 0) {
// console.log(2 + " ");
// console.log('z', 2)
arr.push(2)
n = Math.floor(n / 2);
}
// n must be odd at this point.
// So we can skip one element
// (Note i = i +2)
for (let i = 3; i <= Math.floor(Math.sqrt(n));
i = i + 2) {
// While i divides n, print i
// and divide n
while (n % i == 0) {
arr.push(i)
// console.log('z', i)
// process.stdout.write(i + " ");
n = Math.floor(n / i);
}
}
// This condition is to handle the
// case when n is a prime number
// greater than 2
if (n > 2)
arr.push(n)
// process.stdout.write(n + " ");
// console.log('z', n)
return arr//[...new Set(arr)]
}
``` | 0 | 0 | ['TypeScript'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | 2507. Smallest Value After Replacing With Sum of Prime Factors | 2507-smallest-value-after-replacing-with-h2cf | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | G8xd0QPqTy | NORMAL | 2025-01-19T00:47:54.642164+00:00 | 2025-01-19T00:47:54.642164+00:00 | 3 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```cpp []
class Solution {
public:
int smallestValue(int n) {
vector<int> spf(n + 1);
for (int i = 2; i <= n; ++i) spf[i] = i;
for (int i = 2; i * i <= n; ++i) {
if (spf[i] == i) {
for (int j = i * i; j <= n; j += i) {
if (spf[j] == j) spf[j] = i;
}
}
}
while (true) {
int sum = 0, temp = n;
while (temp > 1) {
sum += spf[temp];
temp /= spf[temp];
}
if (sum == n) break;
n = sum;
}
return n;
}
};
``` | 0 | 0 | ['C++'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | Recursively Easy to Understand | recursively-easy-to-understand-by-yash08-skqa | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | yash080105 | NORMAL | 2025-01-07T12:15:54.598486+00:00 | 2025-01-07T12:15:54.598486+00:00 | 2 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```cpp []
class Solution {
public:
bool isPrime(int n){
if(n==1) return false;
for(int i=2;i<=sqrt(n);i++){
if(n%i==0) return false;
}
return true;
}
int smallestValue(int n) {
if(isPrime(n)) return n;
int sum =0;
for(int i=1;i<sqrt(n);i++){
if(n%i==0 && isPrime(i)){
int m =n;
while(m%i==0){
sum += i;
m /=i;
}
}
}
for(int i=sqrt(n);i>=1;i--){
if(n%i==0 && isPrime(n/i)){
int m=n;
while(m%(n/i)==0){
sum +=(n/i);
m /=(n/i);
}
}
}
if(sum ==n) return n;
return smallestValue(sum);
}
};
``` | 0 | 0 | ['Math', 'Simulation', 'Number Theory', 'C++'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | Beats 100% in C++ | beats-100-in-c-by-nishantjaiswal0001-s69w | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | nishantjaiswal0001 | NORMAL | 2025-01-06T09:10:52.070160+00:00 | 2025-01-06T09:10:52.070160+00:00 | 3 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```cpp []
class Solution {
public:
bool isprime(int n){
for(int i=2;i<=sqrt(n);i++){
if(n%i==0) return false;
}
return true;
}
int sumofprimes(int n){
int sum=0;
for(int i=2;i<sqrt(n);i++){
if(n%i==0 and isprime(i)){
int m=n;
while(m%i==0){
sum+=i;
m/=i;
}
}
}
for(int i=sqrt(n);i>1;i--){
if(n%i==0 and isprime(n/i)){
int m=n;
while(m%(n/i)==0){
sum+=(n/i);
m/=(n/i);
}
}
}
return sum;
}
int smallestValue(int n) {
if(isprime(n)) return n;
n=sumofprimes(n);
if(n==4) return n;
return smallestValue(n);
}
};
``` | 0 | 0 | ['C++'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | Simple Solution | simple-solution-by-ya1125-oj1m | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | Yagnik1125 | NORMAL | 2025-01-03T13:37:54.041932+00:00 | 2025-01-03T13:37:54.041932+00:00 | 3 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->0(nlogn)
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->0(1)
# Code
```cpp []
class Solution {
public:
bool isprime(int n){
if(n==1) return false;
for(int i=2;i<=sqrt(n);i++){
if(n%i==0) return false;
}
return true;
}
int smallestValue(int n) {
if(isprime(n)) return n;
int sum = 0;
for(int i=1;i<sqrt(n);i++){
if(n%i==0 && isprime(i)){
int m=n;
while(m%i==0){
sum+=i;
m/=i;
}
}
}
for(int i=sqrt(n);i>=1;i--){
if(n%i==0 && isprime(n/i)){
int m=n;
while(m%(n/i)==0){
sum+=(n/i);
m/=(n/i);
}
}
}
if(sum==n) return n;
return smallestValue(sum);
}
};
``` | 0 | 0 | ['C++'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | c++ solution 100% beats rate | c-solution-100-beats-rate-by-9031543103-d9i8 | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | 9031543103 | NORMAL | 2025-01-02T17:16:44.738646+00:00 | 2025-01-02T17:16:44.738646+00:00 | 3 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```cpp []
class Solution {
public:
bool isprime(int n) {
if (n == 1) return false;
for (int i = 2; i <= sqrt(n); i++) {
if (n % i == 0)
return false;
}
return true;
}
int smallestValue(int n) {
if (isprime(n)) return n;
int sum = 0;
//kaam
for (int i = 1; i < sqrt(n); i++) {
if (n % i == 0 && isprime(i)) {
int m=n;
while(m%i==0){
sum += i;
m /= i;
}
}
}
for (int i = sqrt(n);i>=1; i--) {
if (n % i == 0 && isprime(n/i)) {
int m=n;
while(m%(n/i)==0){
sum += (n/i);
m /= (n/i);
}
}
}
if (sum == 4) return 4;
return smallestValue(sum);
}
};
``` | 0 | 0 | ['C++'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | easy solution | easy-solution-by-kodzimk-h3z8 | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | Kodzimk | NORMAL | 2025-01-01T15:02:02.147322+00:00 | 2025-01-01T15:02:02.147322+00:00 | 3 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```cpp []
class Solution {
public:
int smallestValue(int n) {
vector<int> div;
int prev = n;
for (int i = 2; true; i++)
{
if (n == i && div.empty())
break;
else if (n % i == 0)
{
div.push_back(i);
n /= i;
while (n % i == 0)
{
n /= i;
div.push_back(i);
}
if (n == 1)
{
n = std::accumulate(div.begin(), div.end(), 0);
div.clear();
i = 1;
if (n == prev)
break;
}
}
}
return n;
}
};
``` | 0 | 0 | ['C++'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | Swift Solution | swift-solution-by-khaledkamal-9f7h | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | khaledkamal | NORMAL | 2024-12-30T23:34:54.643173+00:00 | 2024-12-30T23:34:54.643173+00:00 | 1 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```swift []
class Solution {
private func primeFactors(_ n: Int) -> [Int] {
guard n > 1 else { return [] }
var primes: [Int] = []
var number = n
for i in 2...number {
while number % i == 0 {
primes.append(i)
number /= i
}
}
return primes
}
private func isPrime(_ n: Int) -> Bool {
if n < 2 { return false }
for i in 2 ..< n {
if n % i == 0 { return false }
}
return true
}
func smallestValue(_ n: Int) -> Int {
var n = n
while !isPrime(n) {
let sum = primeFactors(n).reduce(0, +)
if sum == n {
return n
}
n = sum
}
return n
}
}
``` | 0 | 0 | ['Swift'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | Smallest Value After Replacing With Sum Of Prime Factors | smallest-value-after-replacing-with-sum-glxwq | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | ShaksRA | NORMAL | 2024-12-13T20:05:26.774274+00:00 | 2024-12-13T20:05:26.774274+00:00 | 10 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```python3 []\nclass Solution:\n def smallestValue(self, n: int) -> int:\n while 1:\n t, s, i = n, 0, 2\n while i <= n // i:\n while n % i == 0:\n n //= i\n s += i\n i += 1\n if n > 1:\n s += n\n if s == t:\n return t\n n = s\n``` | 0 | 0 | ['Python3'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | Simple to understand | simple-to-understand-by-pitcherpunchst-bjtc | null | pitcherpunchst | NORMAL | 2024-12-11T10:58:12.680566+00:00 | 2024-12-11T10:58:12.680566+00:00 | 2 | false | # Intuition\nbrute force\n# Approach\ncreate a function to check if a number is a prime, and a function to sum the prime factors\ndeal with exception of n=4\nrun a while loop as long as the number doesnt become prime, the sum of prime factors of a prime number is the number itself;\n\n# Code\n```cpp []\nclass Solution \n{\npublic:\nbool isPrime(int n) {\n if (n <= 1)\n return false;\n for (int i = 2; i <= std::sqrt(n); ++i) {\n if (n % i == 0)\n return false;\n }\n return true;\n}\n int sumofprime(int n)\n {\n int sum=0;\n int i=2;\n while(n!=1)\n {\n if(n%i==0)\n {\n sum+=i;\n n/=i;\n }\n else\n i++;\n }\n return sum;\n }\n int smallestValue(int n) \n {\n if(n==4)\n return 4;\n while(!isPrime(n))\n {\n n=sumofprime(n);\n }\n return n;\n }\n};\n``` | 0 | 0 | ['C++'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | Smallest Value After Repeated Prime Factor Sum Replacement | smallest-value-after-repeated-prime-fact-4rvg | Intuition\nThe problem involves continuously replacing a number n with the sum of its prime factors until n becomes a prime number. The intuition behind this ap | user9375Du | NORMAL | 2024-12-01T05:20:58.394741+00:00 | 2024-12-01T05:20:58.394781+00:00 | 3 | false | ## Intuition\nThe problem involves continuously replacing a number `n` with the sum of its prime factors until `n` becomes a prime number. The intuition behind this approach is that by summing the prime factors, we can iteratively reduce the number until it converges to a prime.\n\n## Approach\n1. **Prime Factor Sum Calculation**: \n - Create a helper function `sum_of_prime_factors` to calculate the sum of prime factors of `n`. This involves dividing `n` by its smallest divisors starting from 2 and increasing, ensuring all prime factors are summed.\n \n2. **Repeated Replacement**:\n - Continuously replace `n` with the sum of its prime factors. If the new sum equals the current number `n`, it indicates `n` has become a prime.\n - Use caching to avoid redundant calculations for the same numbers, improving efficiency.\n\n## Complexity\n- **Time complexity**: O (log \uD835\uDC5B \u22C5 \uD835\uDC5B)\n- **Space complexity**: O (n)\n\n## Code\n```python\nclass Solution:\n def smallestValue(self, n: int) -> int:\n def sum_of_prime_factors(n: int) -> int:\n if n < 2:\n return n\n\n total = 0\n while n % 2 == 0:\n total += 2\n n //= 2\n\n factor = 3\n while factor * factor <= n:\n while n % factor == 0:\n total += factor\n n //= factor\n factor += 2\n \n if n > 1:\n total += n\n\n return total\n\n cache = {}\n\n while True:\n if n in cache:\n n = cache[n]\n else:\n new_n = sum_of_prime_factors(n)\n cache[n] = new_n\n\n if new_n == n:\n break\n n = new_n\n\n return n\n``` | 0 | 0 | ['Python3'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | O(sqrt(n) * P(n)) C++ solution [Beats 100% of solutions] | osqrtn-pn-c-solution-beats-100-of-soluti-p6yi | Intuition\nFor any prime number p, its prime factors will be \{1,p\}. Ignoring the trivial factor of 1, the sum of its prime factors will always be equal to p i | Sekqies | NORMAL | 2024-11-27T02:10:39.848625+00:00 | 2024-11-27T02:10:39.848653+00:00 | 0 | false | # Intuition\nFor any prime number $$p$$, its prime factors will be $$\\{1,p\\}$$. Ignoring the trivial factor of $$1$$, the sum of its prime factors will always be equal to $$p$$ itself. Therefore, we can check whether a given number $$n$$ is prime (and therefore satisfies the question) by checking whether $$n=sum(primeFactors(n))$$\n\n# Approach\nFor taking the prime factors of a number efficiently, we can apply a simple prime factorization algorithm. Take the number $$18$$ for example.\n$$18\\mod2=0$$, therefore, $$18 = \\frac{18}{2} \\times2 =9 \\times 2$$\n$$9\\mod2\\neq0$$. We continue\n$$9\\mod3=0$$\n$$...$$\n$$18 = 3\\times3\\times2$$\n\nWe can also show that for any number $$n$$ there will be at most one prime factor greater than $$\\sqrt{n}$$. Since any number can be represented by $$n=a\\times p$$, where $$p$$ is a prime factor of $$n$$ and $a=\\frac{prod(primeFactors(n))}{p}$, $p=\\frac{n}{a}$. If there is any other prime factor greater than $\\sqrt{n}$, $p<\\sqrt{n}$. Therefore, there can be only one $p$ such that $p>\\sqrt{n}$.\n\nFrom that, we can just reduce $n$ with prime factors smaller than $\\sqrt{n}$ and then just add the prime factors obtained with the remainder. That gives us an overall complexity of $P(n)\\times\\sqrt{n}$. Why the $P(n)$? Because that will be the amount of times that function will run; I have no idea whatsoever how much times it will run. I don\'t think anyone does. It\'s related to prime numbers, after all\n\n# Complexity\n- Time complexity: $$O(\\sqrt{n} \\times P(n))$$ \n\n- Space complexity: $$O(1)$$\n\n# About memory\n"Oh, using functions is going to increase memory usage!" and not using them makes the code ugly. Speed is all that matters, folks!\n\n\n# Code\n```cpp []\nclass Solution {\npublic:\n inline int getsum(int n)\n {\n int sum = 0;\n for(int i=2;i*i<=n;i++)\n {\n if(n%i==0)\n {\n while(n%i==0) {\n sum+=i;\n n/=i;\n }\n \n }\n }\n if(n>1) sum += n;\n return sum;\n }\n int smallestValue(int n) {\n while(n!=(n=getsum(n))){}\n\n return n;\n }\n};\n``` | 0 | 0 | ['C++'] | 0 |
smallest-value-after-replacing-with-sum-of-prime-factors | C++ Solution || 100% beat time || Bruteforce | c-solution-100-beat-time-bruteforce-by-v-mqlf | Intuition\n Describe your first thoughts on how to solve this problem. \nSimple Bruteforce implementation.\n\n# Approach\n Describe your approach to solving the | vishalmaurya59 | NORMAL | 2024-11-14T15:05:17.145321+00:00 | 2024-11-14T15:05:17.145377+00:00 | 0 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSimple Bruteforce implementation.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nApplying bruteforce approach. Continuosly finding the sum of the prime factors and checking if the sum is equal to the same value of N or not.\n\nIf N is prime no. then it will take $$sqrt(N)$$ time.\nOtherwise it wiil take $$sqrt(N)$$ + logarithmic time.\n\n# Complexity\n- Time complexity: sqrt(n) + logarithmic time complexity\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n int findsumoffactors(int n){\n int sum=0;\n int x=n;\n for(int i=2;i*i<=x;i++){\n while(n%i==0){\n sum+=i;\n n/=i;\n }\n }\n if(n>1) sum+=n;\n return sum;\n }\n\n int smallestValue(int n) {\n int x;\n while(true){\n x=n;\n n=findsumoffactors(n);\n if(x==n) break;\n }\n return x;\n }\n};\n``` | 0 | 0 | ['C++'] | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.