I need to write a program which counts n digit number sum. For example, sum of 1 digit number is 45 (from 1 to 9) and so on.
I wrote this code but it's useless so I need little bit help.
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
int n;
long suma = 0;
cin >> n;
for (int i = 1; i <= n; i++)
{
suma = suma + i;
}
cout << suma;
return 0;
}
Basically what you're missing is a way to determine what an N digit number is. Fortunately you know that N-digit number ranges from 10^(N-1) to 10^N, so you have this as the loop condition:
#include <cmath>
for (long i = pow(10, n-1); i < pow(10, n); i++)
If you want to do this faster, you need some Maths work beforehand and rework the algorithm. You probably want the summing formula:
a = pow(10, n-1);
b = pow(10, n);
sum = (a + b - 1) * (b - a) / 2; // (a1 + an)* n / 2
You can use the following code to find the sum of N digit numbers:
include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
int n;
long suma = 0;
cin >> n;
for (long i = pow(10, n-1); i < pow(10, n); i++) // this is what was wrong in your approach,
{
suma = suma + i;
}
cout << suma;
return 0;
}
OR
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
int n,x,y;
long suma = 0;
cin >> n;
x =pow(10,n-1);
y =pow(10,n);
suma = (x + (y-1)) * (x - y) / 2; //this will reduce your time complexity from O(n) by eliminating the for loop.
cout << suma;
return 0;
}
Related
My code gives different results on different compilers, the following code gives 499999998352516354 when I enter 1,1000000000 as my input on vs code which is the desired results while it gives 499999998352516352 on codeforces
#include <bits/stdc++.h>
using namespace std;
int main()
{
cout<<fixed<<setprecision(90);
long long x,y;
long long sum=0;
long long z=1;
cin>>x;
for (int i = 0; i < x; i++)
{
cin>>y;
sum=y*(y+1)/2;
z= log2(y);
sum-=2*(pow(2,1+z)-1);
cout<<sum<<"\n";
sum=0;
}
}
Use std::llround() function around pow() and your code will work.
It is because pow() gives floating value which can be incorrectly truncated to 1 less than needed. And llround() gives correct rounding to whole integer.
Below is fixed code, I also adjusted code formatting and changed to correct necessary C++ headers.
Try it online!
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main() {
cout << fixed << setprecision(90);
long long x, y;
long long sum = 0;
long long z = 1;
cin >> x;
for (int i = 0; i < x; i++) {
cin >> y;
sum = y * (y + 1) / 2;
z = log2(y);
sum -= 2 * (llround(pow(2, 1 + z)) - 1);
cout << sum << "\n";
sum = 0;
}
}
Input:
1 1000000000
Output:
499999998352516354
As it is suggested in comments you may also use bit shifting 1LL << x instead of pow(2, x) if x is non-negative integer. And instead log2(y)
if y is integer then one can use std::bit_width(y) - 1 (read about std::bit_width)
Try it online!
#include <iostream>
#include <iomanip>
#include <cmath>
#include <bit>
using namespace std;
int main() {
cout << fixed << setprecision(90);
long long x, y;
long long sum = 0;
long long z = 1;
cin >> x;
for (int i = 0; i < x; i++) {
cin >> y;
sum = y * (y + 1) / 2;
z = std::bit_width<unsigned long long>(y) - 1;
sum -= 2 * ((1LL << (1 + z)) - 1);
cout << sum << "\n";
sum = 0;
}
}
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;
}
#include <iostream>
#include <algorithm>
using std::cin;
using std::cout;
using std::endl;
using std::sort;
int main()
{
int x = 0;
int n; // Enter Size of 2 array
cin >> n; // enter 5
long long *ptr1 = new long long[n - 1]; // size of array must be less than 5 by one n-1
for (int x = 0; x < n - 1; x++)
{
cin >> ptr1[x];
}
sort(ptr1, ptr1 + (n - 1));
for (int z = 1; z < n; z++)
{
if (z != ptr1[x])
{
cout << z;
break;
}
x++;
}
return 0;
}
You're given all positive integers from 1,2,…,n except one integer. Find the missing integer.
Input
The first line of input contains an integer n (2≤n≤2×105).
The second line of input contains n−1 distinct integers from 1 to n (inclusive).
Output
Print the missing integer.
when i try to sumbit this code i get wrong in test 10 but i don't know why! and he didn't show the test so what is wrong?
I have a three-fold answer:
This program leaks memory
You included <algorithm> please use it. (Look on cppreference)
Spoilers vector, iota, mismatch
Also, you don't reset x before the second loop.
That's never going to work unless the missing integer is the last of the array, and not equal to 1
// for (... z)
if (z != ptr1[x] /* Here */) {
// print 1, end loop OR invoke undefined behavior
}
x++; // Now x is equal to (n - 1)
There are some problems with your code:
long long *ptr1 = new long long[n - 1];
You call new, without delete. This will create a memory leak.
You haven't reinitialized x, so any access to ptr1[x] is out-of-bounds.
Slove all of that, and your code will look like:
#include <iostream>
#include <algorithm>
#include <vector>
int main()
{
int n;
cin >> n;
std::vector<int> vec(n-1); // std::vector instead
for (int x = 0; x < n - 1; x++)
{
std::cin >> vec[x];
}
std::sort(vec.begin(), vec.end());
int x = 0; // use another variable instead of reused the old one.
for (int z = 1; z < n; z++)
{
if (z != vec[x])
{
std::cout << z;
break;
}
x++;
}
return 0;
}
But, this isn't the best approach anyway. As #molbdnilo suggest:
#include <iostream>
int main()
{
int n;
std::cin >> n;
int sum{};
for (int i = 0; i < n - 1; ++i)
{
int tmp;
std::cin >> tmp;
sum += tmp;
}
std::cout << (n + 1) * n / 2 - sum;
}
You can solve this in a more straightforward way if you subtract the numbers that were entered from the expected sum of all numbers 1, 2, ..., n (see comments by #molbdnilo, #Aconcagua and #marcus-müller):
#include <iostream>
int main() {
std::size_t n;
std::cin >> n;
std::size_t sum{ n*(n + 1)/2 };
for (std::size_t idx = 1; idx != n; ++idx) {
std::size_t thisNumber;
std::cin >> thisNumber;
sum -= thisNumber;
}
std::cout << "Missing number: " << sum << std::endl;
}
Building blocks:
The expected sum: int expected_sum = n * (n + 1) / 2;
The sum of all integers fed to the test after you've read n:
#include <iterator> // istream_iterator
#include <numeric> // accumulate
int sum = std::accumulate(std::istream_iterator<int>(std::cin),
std::istream_iterator<int>{}, 0);
Now, expected_sum - sum should give you the missing value.
Heres the problem:
A Milkman serves milk in packaged bottles of varied sizes. The possible size of the bottles are {1, 5, 7 and 10} litres. He wants to supply desired quantity using as less bottles as possible irrespective of the size. Your objective is to help him find the minimum number of bottles required to supply the given demand of milk.
Input Format:
First line contains number of test cases N
Next N lines, each contain a positive integer Li which corresponds to the demand of milk.
Output Format:
For each input Li, print the minimum number of bottles required to fulfill the demand
I have written this code for the problem.
#include <iostream>
#include <vector>
#include <stdio.h>
#include <set>
#include <algorithm>
#include <string>
#include <queue>
#include <map>
#include <iomanip>
#include <locale>
#include <stdlib.h>
#include <cstring>
#include <cmath>
#include <tgmath.h>
using namespace std;
const int INF = 1000000000;
int m[4] = { 1, 5, 7, 10 };
int r[100000000];
int milk(int n) {
int q;
if (r[n] < INF)
return r[n];
if (n <= 0)
q = 0;
else {
q = INF;
for (int i = 0; i < 4; i++) {
if (n >= m[i])
q = min(q, 1 + milk(n - m[i]));
}
}
r[n] = q;
return q;
}
int main() {
int t, n;
cin >> t;
while (t--) {
cin >> n;
memset(r, INF, sizeof(r));
cout << milk(n) << endl;
}
return` 0;
}
I have used dynamic programming for this.But I only get an output zero for every input.I am new to dp.Please help.
int milk(int n) {
int min;
memset(r, 0, sizeof(r));
for (int i = 1; i <= n; i++) {
min = 10000000; // some very large value greater than n
for (j = 0; j <= 3; j++) {
if (m[j] > i)
continue;
else if (r[i - m[j]] < min)
min = r[i - m[j]];
}
r[i] = min;
}
return r[n];
}
I hope this works and it would help you , comment if you have any doubts.
Here is the question:
Comparing two numbers written in index form like 2^11 and 3^7 is not difficult, as any calculator would confirm that 2^11=2048<3^7=2187.
However, confirming that 632382^518061>519432^525806 would be much more difficult, as both numbers contain over three million digits.
You are given N base exponent pairs, each forming a large number you have to find the Kth smallest number of them. K is 1−indexed.
Input Format
First line containts an integer N, number of base exponent pairs. Followed by N lines each have two space separated integers B and E, representing base and exponent.
Last line contains an integer K, where K<=N
Constraints
1≤N≤105
1≤K≤N
1≤B≤109
1≤E≤109
No two numbers are equal.
Here is my code:
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int N,i = 0,k,j=0,x,m;
long long int *arr,*arr2,*arr3;
cin >> N;
arr = (long long int *)malloc(sizeof(long long int)*2*N);
arr2 = (long long int *)calloc(N,sizeof(long long int));
arr3 = (long long int *)calloc(N,sizeof(long long int));
x = 2*N;
while(x>0)
{
cin >> arr[i];
i++;
x--;
}
cin >> k;
for(i=0;i<2*N;i+=2)
{
arr2[j] = pow(arr[i],arr[i+1]);
j++;
}
arr3 = arr2;
sort(arr2,arr2+N);
for(i=0;i<N;i++)
{
if(arr3[i] == arr2[k-1])
{
m = i;
break;
}
}
cout << arr[2*m] << " " << arr[2*m + 1];
return 0;
}
The program works for small numbers only, can't make it work for large numbers. what to do?
Maybe you are generating an overflow on the big numbers. You could consider using a multiprecision arithmetic library such as https://gmplib.org/. I haven't used this library myself.
Have a look at this post How to detect integer overflow? on how to detect integer overflow.
From your choosing of long long int type I guess you calculated the a^b of the numbers in order to sort them, which leads to very big numbers and may lead to overflow.
Note that in order to sort the numbers there is no need for this calculation, for knowing if a^b > d^c it is sufficient to check log(a^b) > log(c^d) and therefore b*log(a) > d*log(c).
And it's better to use a struct or class to create a data structure for this big numbers.
This is the code for it:
#include <iostream>
#include <algorithm>
#include <math.h>
using namespace std;
struct BigNumber{
int base;
int exponent;
};
int Compare(BigNumber x, BigNumber y);
void Sort(BigNumber* arr, int N);
int main() {
int N,i = 0,k;
BigNumber *numbers;
cout<<"\nEnter N:";
cin >> N;
numbers = (BigNumber *)calloc(N,sizeof(BigNumber));
for(i=0; i<N; i++)
{
cout<<"\nEnter base and exponent for number "<<i<<":";
cin >> numbers[i].base>>numbers[i].exponent;
}
cout<<"\nEnter K:";
cin >> k;
Sort(numbers,N);
cout << "Kth number is :" << numbers[k].base << "^" << numbers[k].exponent;
return 0;
}
void Sort(BigNumber* arr, int N){
for(int i=0; i< N; i++ ){
for(int j=0; j< N; j++){
if(Compare(arr[i], arr[j])<0){
BigNumber temp = arr[j];
arr[j] = arr[i];
arr[i] = arr[j];
}
}
}
}
int Compare(BigNumber x, BigNumber y){
double X = x.exponent * log10(x.base);
double Y = y.exponent * log10(x.base);
return X == Y? 0: X > Y ? 1: -1;
}
I changed the code a little. Only problem I was having was I was calculating the exponent rather than comparing log of exponent.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int N,i = 0,k,j=0,x,m;
int *arr;
double *arr2,*arr3;
cin >> N;
arr = (int *)malloc(sizeof(int)*2*N);
arr2 = (double *)calloc(N,sizeof(double));
arr3 = (double *)calloc(N,sizeof(double));
x = 2*N;
while(x>0)
{
cin >> arr[i];
i++;
x--;
}
cin >> k;
for(i=0;i<2*N;i+=2)
{
arr2[j] = arr[i+1]*log10(arr[i]);
j++;
}
for (i = 0; i < N; i++) {
arr3[i] = arr2[i];
}
sort(arr2,arr2+N);
for(i=0;i<N;i++)
{
if(arr3[i] == arr2[k-1])
{
m = i;
break;
}
}
cout << arr[2*m] << " " << arr[2*m + 1];
return 0;
}