Sum of factoriais to be equal a given number - c++

I'm trying to solve the following problem:
What is the smallest number of factoriais summed that are needed to be equal an given number a? (1 ≤ a ≤ 10^5)
Example:
Input: 10, Output: 3. (10 = 3! + 2! + 2!)
Input: 25, Output: 2. (25 = 4! + 1!)
My code:
#include<bits/stdc++.h>
using namespace std;
int a;
int rec(int vet){
int count = 0;
a = a - vet;
if(a >= vet){
count++;
rec(vet);
}
count++;
return count;
}
int main(){
int vet[8] = {1}, count = 0;
cin >> a;
for(int i = 2; i <= 8; i++){
vet[i-1] = vet[i-2]*i;
}
for(int i = 7; i >= 0; i--){
if(a < vet[i]){
continue;
}
count += rec(vet[i]);
}
cout << count << endl;
}
My logic:
1°: a max is equal to 100000, so the maximum fatorial we have to
compare is 8!;
2°: I take a factioral that is equal or nearest small to a,
subtract the factorial from it and count++; If after the subtraction,
a still bigger then my factorial, I do the same step recursively.
This code pass on the base cases, but I got a wrong answer. I wasn't capable to find what case it didn't pass, so I'm here.
Can you find where am I wrong? Or if my solution is not good and I should try another approach.
Thanks for the help!

The problem is easily solved by a recursive approach.
Here is checked code:
#include <iostream>
using namespace std;
int factorial(int n) {
return n<=1 ? 1 : n * factorial(n-1);
}
int MinFact(int number)
{
static int num_of_facts;
int a = 1;
if (number)
{
while(factorial(a+1)<=number)a++;
cout << a << "!" << endl;
num_of_facts++;
MinFact((number-factorial(a)));
}
return num_of_facts;
}
int main()
{
int num;
cout << "Enter number" << endl;
cin >> num;
num = MinFact(num);
cout << "Number of factorials: " << num;
return 0;
}

As I mentioned in the comment, the issue is with the rec function. Due to rec being local, the count is not being incremented correctly.
A simple solution would be to replace the rec function as follows
int rec(int vec) {
int count = a / vec;
a = a % vec;
return count;
}
Edit : for a failing case try 18. The solution will be 3 but you will get 2.
I guess you can figure out how this logic works. If not you could do it with a loop.

Related

Recursive Digit Sum

I was trying to solve this problem on hackerrank. But I got some problem. Specific problem is:
For example:
The sum of digits 9875 will be calculate as: sum(9875) = 9+8+7+5 = 29. sum(29) = 11. sum(11) = 2. (Using recursive function).
In my test case, (n ='9875', k=4) the number p is created by concatenating the string n k times so the initial p = 9875987598759875 ( the string '9875' repeat 4 times ).
But when i code this test case, it doesn't work. Here is my source code:
int SuperDigit(long n){
long sum =0;
if(n==0) return 0;
else{
return sum= sum +(n%10 + SuperDigit(n/10));
}
if(sum>10){
return (sum%10 + SuperDigit(sum/10));
}
}
int main(){
string n;cin>>n;
int T;cin>>T;
string repeat;
for(int i=0; i <T;i++){
repeat += n;
}
cout<<repeat;
long x=0;
stringstream geek(repeat);
geek>>x;
long sum = SuperDigit(x);
printf("\n%ld ",sum);
for(int i=0;i<10;i++){
if(sum>=10){
sum = SuperDigit(sum);
}
else{
break;
}
}
printf("\n%ld ",sum);
}
If i try: n = '123' and k =3 (Expected output: 9)
My output will be correct, here is my output for this test case:
123 3
123123123
18
9
But when i try n = '9875' and k = 4 (Expected output: 8)
My output will be wrong:
9875 4
9875987598759875
46
1
As you can see in this test case, the first sum of all digits must be 116. But mine only show 46. Can anyone explain for me? Thanks a lot!
In your current code you return prematurely in
if(n==0) return 0;
else{
return sum= sum +(n%10 + SuperDigit(n/10));
}
Imagine that n == 89 so n%10 returns 9 and SuperDigit(n/10) returns 8 and you have 17 as an answer (when 8 is expected).
You can put it as
int SuperDigit(long n) {
int result = 0;
/* We compute digital root (sum of digits) */
for (long number = n; number != 0; number /= 10)
result += (int) (number % 10);
/* if result is out of range [-9..9]
we compute digital root again from the answer */
if (result < -9 || result > 9)
result = SuperDigit(result);
return result;
}
You can simplify your program as shown below. Since you want to find the sum recursively, the below program shows one possible way of doing it.
Version 1: Using recursive function
#include <iostream>
int findDigit(int passed_num, int currentSum)
{
int lastDigit;
if (passed_num == 0) {
return currentSum;
}
// find the last didit
lastDigit = passed_num % 10;
currentSum+= lastDigit;
//call findDigit() repeatedly
currentSum = findDigit(passed_num / 10, currentSum);
std::cout<<lastDigit<<" ";
return currentSum;
}
int main()
{
std::cout << "Enter a number: ";
int input_num, sum;
std::cin>>input_num;
sum = findDigit(input_num, 0);
std::cout<<"sum is: "<<sum<<std::endl;
std::cout << "Enter another number: ";
std::cin>>input_num;
sum = findDigit(input_num, 0);
std::cout<<"sum is: "<<sum<<std::endl;
return 0;
}
Note there are simpler(other) ways of finding the sum without recursively. One such way is shown below:
Version 2: Using loop
#include <string>
#include <iostream>
int main()
{
std::cout << "Enter a number: ";
int individual_number = 0, sum = 0;//these are local built in types so initialize them
std::string input_num;
std::cin >> input_num;
for(char c : input_num)
{
individual_number = c -'0';
std::cout<<individual_number<<" ";
sum+= individual_number;
}
std::cout<<"total amount: "<<sum<<std::endl;
// std::cout<<"The sum comes out to be: "<<sum<<std::endl;
return 0;
}

Tips on Improving Efficiency of this Code (Beginner)

I am currently doing a coding exercise and am missing some cases due to the time limit being exceeded. Can I get some tips on how to improve the efficiency of my code? Also if you have any general tips for a beginner I would also appreciate that. The problem is below and thanks.
You are given all numbers between 1,2,…,n except one. Your task is to find the missing number.
Input
The first input line contains an integer n.
The second line contains n−1 numbers. Each number is distinct and between 1 and n (inclusive).
Output
Print the missing number.
Constraints
2≤n≤2⋅105
Example
Input:
5
2 3 1 5
Output:
4
Here is my code:
#include <bits/stdc++.h>
using namespace std;
int missingNumber(vector<int> available, int N) {
for (int i=1; i<=N; i++) {
bool counter = false;
for (int j=0; j<N-1; j++) {
if (i == available[j]) {
counter = true;
}
}
if (counter == false) {
return i;
}
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int N;
cin >> N;
vector<int> available(N-1);
int temp = 0;
for (int i=0; i<N-1; i++) {
cin >> temp;
available[i] = temp;
}
cout << missingNumber(available, N);
}
A very simple solution with O(N) complexity is based on the observation that if the N-1 numbers are all between 1 and N and distinct from each other, then it suffices to:
compute the sum of all these N-1 numbers, so linear complexity
subtract the sum computed at step 1 from the sum of the N numbers from 1 to N, which we know is N * (N + 1) / 2, so O(1) complexity.
here is an answer with two versions to your problem
the first version is using Arithmetic progression formula n*(a1 + an) /2
and then subtract your vector sum with the result of the formula.
double missingNumber_ver1(std::vector<int> available, int N) {
// formula for sum for Arithmetic progression
double sum = N * (available[0]+available[N-2]) /2;
double available_sym = std::accumulate(available.begin(), available.end(), 0); // this is to sum the giving numbers
double missing_num = sum-available_sym;
return missing_num;
}
the second version is to use XOR operator and when there is a xor value that is not 0 that means this is the missing number. I'm also using std::iota to build the comparison vector with range values.
double missingNumber_ver2(std::vector<int> available, int N) {
std::vector<int>tem_vec(N-1);
std::iota(tem_vec.begin(), tem_vec.end(), available[0]);
auto av_it = available.begin();
auto tem_vec_it = tem_vec.begin();
while(!(*av_it ^ *tem_vec_it))
{
av_it++;
tem_vec_it++;
}
return *tem_vec_it;
}
and here is the full code - look that I made few changes also in the main() function
#include <iostream>
#include <numeric>
#include <vector>
double missingNumber_ver1(std::vector<int> available, int N) {
// formula for sum for Arithmetic progression
double sum = N * (available[0]+available[N-2]) /2;
double available_sym = std::accumulate(available.begin(), available.end(), 0);
double missing_num = sum-available_sym;
return missing_num;
}
double missingNumber_ver2(std::vector<int> available, int N) {
std::vector<int>tem_vec(4);
std::iota(tem_vec.begin(), tem_vec.end(), available[0]);
auto av_it = available.begin();
auto tem_vec_it = tem_vec.begin();
while(!(*av_it ^ *tem_vec_it))
{
av_it++;
tem_vec_it++;
}
return *tem_vec_it;
}
int main() {
int N;
std::cin >> N;
std::vector<int> available;
int temp = 0;
for (int i=0; i<N-1; i++) {
std::cin >> temp;
available.push_back(temp);
}
std::cout << "missingNumber_ver1 " << missingNumber_ver1(available, N) << "\n";
std::cout << "missingNumber_ver2 " <<missingNumber_ver2(available, N) << "\n";
}

What's wrong with my solution to Project Euler's #2 in C++?

This is the question:
Each new term in the Fibonacci sequence is generated by adding the
previous two terms. By starting with 1 and 2, the first 10 terms will
be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... By considering the terms in
the Fibonacci sequence whose values do not exceed four million, find
the sum of the even-valued terms.
When I run the program below it gives me
-1833689714
Can someone kindly help me out with the code?
========================================================================
#include <iostream>
using namespace std;
int fibona (int k);
int first = 0 , second = 1 , fibo = 0, sum = 0;
int main()
{
cout << "Sum of even values less than 4000000 : " ;
fibona (4000000);
}
int fibona (int k)
{
for (int c = 0 ; c < k ; c++)
{
if (c <= 1)
{
fibo = c;
}
else
{
fibo = first + second;
first = second;
second = fibo;
}
if (fibo % 2 == 0)
{
sum += fibo;
}
}
cout << sum <<endl;
}
Do you know how much is Fib(4000000)?
Must be approximately
((1+Sqrt[5])/2)^4000000/Sqrt[5] = 1.627477... × 10^835950
There is no way to fit it in any variable type.
#include <iostream>
using namespace std;
long fibona_even_sum (long k);
int main()
{
const long N=4000000;
cout << "Sum of even Fibonacci numbers: " <<endl;
cout << fibona_even_sum(N) <<endl;
}
long fibona_even_sum(long N_max)
{
long first = 0 , second = 1;
long sum=0;
while(true)
{
long first_copy=first;
first=second;
second+=first_copy;
if(second>N_max)
return sum;
if(!(second%2))
sum+=second;
}
}
In the code above you can change all long to int. It works fine.

C++: find first prime number larger than a given integer

Question: How to find, for a given integer n, the first prime number that is larger than n?
My own work so far
I've managed to write a program that checks whether or not a given integer is a prime or not:
#include <iostream>
#include <cmath>
using namespace std;
bool is_prime (int n)
{
int i;
double square_root_n = sqrt(n) ;
for (i = 2; i <= square_root_n ; i++)
{
if (n % i == 0){
return false;
break;
}
}
return true;
}
int main(int argc, char** argv)
{
int i;
while (true)
{
cout << "Input the number and press ENTER: \n";
cout << "To exit input 0 and press ENTER: \n";
cin >> i;
if (i == 0)
{
break;
}
if (is_prime(i))
cout << i << " is prime" << endl;
else
cout << i << " isn't prime'" << endl;
}
return 0;
}
I'm struggling, however, on how to proceed on from this point.
You have a function is_prime(n), and a number n, and you want to return the smallest number q such that is_prime(q)==true and n <= q:
int q = n;
while (!is_prime(q)) {
q++;
}
// here you can be sure that
// 1. q is prime
// 2. q >= n -- unless there was an overflow
If you want to be a bit more efficient, you can check explicitly for the even case, and the increment by 2 each time.
It's a concrete example of a general theme: if you have a test function and a method for generating elements, you can generate the elements that pass the test:
x = initial_value
while (something) {
if (test(x)) {
// found!
// If you only want the first such x, you can break
break;
}
x = generate(x)
}
(note that this is not a valid C++ code, it's pseudocode)
int i;
**int k_koren_od_n = (int)(sqrt(n) + 0.5)**
for (i = 2; i <= k_koren_od_n ; i++){
To get around casting issues, you might want to add this fix.

C++ factorial of 0

I am writing a program to find the factorial of a user inputted number. My program works from, except for finding the factorial of 0. The requirement is that the factorial of 0 should output one, but I cannot think of a way to write this capability into the code without creating a special case for when 0 is entered. This is what I have so far
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int startingNumber = 0;
double factorialize = NULL;
while(startingNumber != -1) {
cout << "Enter the numbr to factorial: ";
cin >> startingNumber;
factorialize = startingNumber;
for(int x=startingNumber-1;x>=1;x--) {
factorialize = factorialize*x;
}
cout << factorialize << endl;
factorialize = NULL;
}
return 0;
}
This outputs a factorial accurately for all cases except 0. Is there a way to do this that doesn't require a special case? I am thinking no because when I read about the reasons for why 0! is 1, it says that it is defined that way, in other words, you cannot reason your way into why it is 1. Just like x^0, 0! = 1 has a different logic as to why than why 2^2 is 4 or 2! = 2.
try this:
factorialize = 1;
for(int x=2; x<=startingNumber;x++)
factorialize *= x;
Try this:
for (unsigned int n; std::cin >> n; )
{
unsigned int result = 1;
for (unsigned int i = 1; i <= n; ++i) { result *= i; }
std::cout << n << "! = " << result << "\n";
}
You can change the result type a bit (unsigned long long int or double or long double), but ultimately you won't be able to compute a large number of factorials in hardware.
First of all I do not see how it can be calculated accurately, as you multiply startingNumber twice. So just fix the logic with:
factorialize = 1.0;
for(int x=startingNumber;x>=1;x--) {
factorialize = factorialize*x;
}
And it should calculate factorial properly as well as handling 0 the proper way.
Also you should not use NULL as initial value for double, it is for pointers.
There is a complete factorial of number program of C++ which includes the facility of factorial of positive number,negative and zero.
#include<iostream>
using namespace std;
int main()
{
int number,factorial=1;
cout<<"Enter Number to find its Factorial: ";
cin>>number;
if(number<0
)
{
cout<<"Not Defined.";
}
else if (number==0)
{
cout<<"The Facorial of 0 is 1.";
}
else
{
for(int i=1;i<=number;i++)
{
factorial=factorial*i;
}
cout<<"The Facorial of "<<number<<" is "<<factorial<<endl;
}
return 0;
}
You can read full program logic on http://www.cppbeginner.com/numbers/how-to-find-factorial-of-number-in-cpp/
The function listed below returns the factorial FASTER than any solution posted here to this date:
const unsigned int factorial(const unsigned int n)
{
unsigned int const f[13] = { 1,1,2,6,24,120,720,5040,40320,362880,3628800,39916800,479001600 };
return f[n];
}
I looks silly but it works for all factorials that fit into a 32-bit unsigned integer.