How can I make this structure non-repetitive? - c++

I need to make the following structure in a non-repetitive structure. Like the statement to run just one time. I tried to put "if" but that shows me the result 0.
I need to change this:
#include <iostream>
using namespace std;
int main() {
int n, i, s, x;
cin >> n;
s=0;
x=2;
for (i=1;i<=n;i++) {
s = s+x;
x = x+2;
}
cout << s;
return 0;
}
I tried this:
#include <iostream>
using namespace std;
int main() {
int n, i, s, x;
cin >> n;
s=0;
x=2;
if (i>=1 && i<=n) {
s = s+x;
x = x+2;
}
cout << s;
return 0;
}

The value to add (x) will be 2, 4, 6, 8, ....
This is an arithmetic progression and there is a formula to calculate the sum of specified number of elements.
Using the formula S_n = n/2 [2a_1 + (n-1)d] with a_1 = 2 and d = 2, the answer is:
#include <iostream>
using namespace std;
int main() {
int n, s;
cin >> n;
if (n >= 1) {
// s = n * (2 * 2 + (n - 1) * 2) / 2;
// s = n * (2 + (n - 1));
s = n * (n + 1);
} else {
s = 0;
}
cout << s;
return 0;
}

Related

Trying to implement Willans' formula for the n-th prime, what's the problem with my code?

The formula is listed in the following article: https://en.wikipedia.org/wiki/Formula_for_primes. I am trying to implement it but to no success, for whatever reason the code is producing number which seem to be nth power of two + 1, which is obviously not what I want to achieve.
#include <iostream>
#include <cmath>
using namespace std;
int nth_prime(int n) {
double s = 1;
for (int i = 1; i <= pow(2, n); i++) {
double c = 0;
for (int j = 1; j <= i; j++) {
double f = (tgamma(j)+1)/j;
c+=floor(pow(cos(M_PI*f), 2));
}
s+=floor(pow(n/c, 1/n));
}
return s;
}
int main() {
int n;
while (cin >> n) {
cout << nth_prime(n) << endl;
}
return 0;
}

find minimum number of ways to write the sum of square of value from 0 to n equals n

#include<bits/stdc++.h>
using namespace std;
vector<int> memo;
class Solution{
public:
int minimum(int a , int b){
if(a>b) return b;
return a;
}
public:
int MinSquares(int n , vector<int> memo)
{
if(n<= 3){
return n;
}
if(memo[n]>-1) {
return memo[n];
}
int m = n ;
for(int i = 1 ; n-(i*i)>=0; i++){
m = minimum(m, MinSquares(n - i*i , memo) + 1 );
}
memo[n]=m;
return memo[n];
}
};
// { Driver Code Starts.
int main(){
int tc;
cin >> tc;
while(tc--){
int n;
cin >> n;
Solution ob;
vector<int> memo;
memo.assign(n+1 ,-1);
memo[0]=0;
memo[1]=1;
int ans = ob.MinSquares(n , memo);
cout << ans <<"\n";
}
return 0;
}
I know that recurrence relation and tried solving it with using pen and paper.
when trying output 100 it taking much more time than usual . I have spend at least 4 hour why there is no error in syntax and it is giving result for less than 100 .
please help.
It is not clear what you want to do. You claimed total number of ways, but in practice, you code calculates the minimum length of a sum of squares equal to n
Concerning the efficency issue that you mentioned, the problem is here:
int MinSquares(int n , std::vector<int> memo)
You are continuoulsly copying memo. This is solved by using a reference
int MinSquares(int n , std::vector<int>& memo)
#include <iostream>
#include <vector>
//vector<int> memo;
class Solution{
public:
int minimum(int a , int b){
if(a>b) return b;
return a;
}
public:
int MinSquares(int n , std::vector<int>& memo) {
if (n <= 3){
return n;
}
if(memo[n] > -1) {
return memo[n];
}
int m = n ;
for(int i = 1 ; n-(i*i)>=0; i++){
m = minimum (m, MinSquares(n - i*i , memo) + 1);
}
memo[n] = m;
return memo[n];
}
};
// { Driver Code Starts.
int main(){
int tc;
std::cin >> tc;
while(tc--){
int n;
std::cin >> n;
Solution ob;
std::vector<int> memo;
memo.assign(n+1 ,-1);
memo[0] = 0;
memo[1] = 1;
int ans = ob.MinSquares(n , memo);
std::cout << ans <<"\n";
}
return 0;
}

How to write combinations recursively in C++

I wrote a program for finding combination(n Choose r = nCr) using for loop/iterations, wanted to know how to do the same using recursion.
Code is as follows:
#include<iostream>
using namespace std;
int main(){
int n,r;
float num = 1,denum = 1,comb = 1;
cout<<"Enter the values of n and r in nCr \n";
cin>>n>>r;
for (int i = 1; i <= r; i++)
{
num *= (n-r+i);
}
for (int i = 1; i <= r; i++)
{
denum *= (i);
}
comb = num/denum;
cout<<"The number of combinations is "<<comb<<"\n";
}
The following code that I've written helps in finding nCr through recursion:
#include<iostream>
using namespace std;
float comb(int n,int r){
if(r!=0)
{
return (n-r+1)*comb(n,r-1)/r;
}
else
{
return 1;
}
}
int main(){
int n,r;
float com;
cout<<"Enter the values of n and r in nCr \n";
cin>>n>>r;
if(n-r>=r)
{
com = comb(n,r);
}
else
{
com = comb(n,n-r);
}
cout<<"The number of combinations is "<<com<<"\n";
}
Had done this program recently, upon calling the com function in main(), the function is calling itself(i.e recurses) until r value becomes 0 after which it goes to the base statement i.e return 1 if r equals 0
If you just need a code, here it is
int findNumerator(int num, int i, int r) {
return num * (i != r ? findNumerator(num, i+1, r) : 1);
}
int findDenominator(int denum, int i, int r) {
return denum * (i != r ? findDenominator(denum, i+1, r) : 1);
}
int main(){
int n,r;
float num = 1,denum = 1,comb = 1;
cout<<"Enter the values of n and r in nCr \n";
cin>>n>>r;
comb = findNumerator(num, 1, r) / findDenominator(denum, 1, r);
cout<<"The number of combinations is "<<comb<<"\n";
}

difference of sum of even and odd places in an array using recursion.

I am trying to create a recursive function which returns elements even sum,odd sum,difference b/w even and odd for an array.
I created an object having these 3 values and have passed it by reference,the syntax is giving no error,but I am getting run time exception(the run window stopped working)
#include < iostream >
using namespace std;
class o_e_sum
{
public:
int e_sum;
int o_sum;
int diff;
o_e_sum()
{
e_sum = 0;
o_sum = 0;
diff = 0;
}
};
void fib(int*,int, o_e_sum& );
int main()
{
int n;
cin >> n;
int * p = new int[n];
for (int i = 0; i < n; i++)
{
cin >> * (p + i);
}
o_e_sum obj;
fib(p, n, obj);
cout << obj.diff;
}
void fib(int * n, int s, o_e_sum & ques)
{
if (s == 1)
{
ques.o_sum = * n;
ques.diff = ques.e_sum - ques.o_sum;
}
if (s % 2 == 0)
{
fib(n + 1, s - 1, ques);
ques.e_sum = ques.e_sum + * n;
ques.diff = ques.e_sum - ques.o_sum;
}
else
{
fib(n + 1, s - 1, ques);
ques.o_sum = ques.o_sum + * n;
ques.diff = ques.e_sum - ques.o_sum;
}
}

How to select all possible combination of elements from a set using recursion

This is a question from hackerrank; I am trying to understand how recursion works.
The task at hand is:
Find the number of ways that a given integer, X, can be expressed
as the sum of the Nth power of unique, natural numbers.
So for example, if X = 100 and N = 2
100 = 10² = 6² + 8² = 1² + 3² + 4² + 5² + 7²
so 100 can be expressed as the square of unique natural numbers in 3
different ways, so our output is 3.
Here is my code,:
#include <cmath>
#include <iostream>
using namespace std;
int numOfSums(int x, int& n, const int k) {
int count = 0, j;
for (int i = (k + 1); (j = (int) pow(i, n)) <= x; i++) {
j = x - j;
if (j == 0)
count++;
else
count += numOfSums(j, n, i);
}
return count;
}
int main() {
int x, n;
cin >> x >> n;
cout << numOfSums(x, n, 0) << endl;
return 0;
}
But when I input x = 100 and n = 2, it's outputting 2, not 3. What's wrong with the code?
Link to the question: https://www.hackerrank.com/challenges/the-power-sum
Your example code returns 3 when I run it using this main():
#include <iostream>
int main() {
int x = 100, n = 2;
cout << numOfSums(x, n, 0) << endl;
return 0;
}
The problem is likely that you're using double std::pow(double, int), but you're not rounding the result to nearest integer ((int) casts round down). You should add ½ before truncating:
j = static_cast<int>(pow(i, n) + 0.5)
I've used the more-C++ style of cast, which I find clearer.
It would be more efficient to implement your own equivalent of std::pow() that operates on integers. That can be recursive, too, if you want:
unsigned long pow(unsigned long x, unsigned long n)
{
return n ? x * pow(x, n-1) : 1;
}
An iterative version is more efficient (or a tail-recursive version and suitable optimizing compiler).
Reduced version, with my changes:
template<typename T>
T powi(T x, T n)
{
T r{1};
for (; n; n /= 2) {
r *= n%2 ? x : 1;
x *= x;
}
return r;
}
template<typename T>
T numOfSums(T x, T n, T i = {})
{
T count{}, j;
for (++i; (j = powi(i, n)) <= x; ++i)
count += j == x ? 1 : numOfSums(x-j, n, i);
return count;
}
#include <iostream>
int main()
{
unsigned long int x = 100, n = 2;
std::cout << numOfSums(x, n) << std::endl;
return 0;
}