This question already has answers here:
std::pow with integer parameters, comparing to an integer type
(4 answers)
Closed 5 years ago.
I have written a short C++ function which is trying to return similar to the the radical of an integer, but instead the smallest integer which is some root of the the given number. The general logic is that the function decomposes the integer into prime powers, looks for the gcd of all the exponents of the prime powers, then finds the radical by dividing all the exponents by this gcd. I thought the function was working as intended, however I noticed that when passing the function 13 as an argument it returns 12, I have put the output for 2 to 20 below; everything is correct apart from 13. When trying to debug this both "primes" and "powers" hold the correct values (13 and 1 respectively, both vectors of size 1) as well as "power" being 1 as it should. So the problem must be in the last loop, but I am really very confused as to what could be happening as it should be calculating pow(13, 1/1).
#include<vector>
#include<cmath>
int radical(int n){
int power = 0;
std::vector<int> primes;
std::vector<int> powers;
for(int i = 2; n != 1 ; ++i){
int t_pow = 0;
while(n % i == 0){
++t_pow;
n /= i;
}
if(t_pow != 0){
primes.push_back(i);
powers.push_back(t_pow);
}
power = (power == 0 ? t_pow : gcd(power, t_pow));
}
int rad = 1;
for(unsigned i = 0u; i < primes.size(); ++i){
rad *= pow(primes.at(i), powers.at(i)/power);
}
return rad;
}
2 2
3 3
4 2
5 5
6 6
7 7
8 2
9 3
10 10
11 11
12 12
13 12
14 14
15 15
16 2
17 17
18 18
19 19
20 20
EDIT: The most efficient way to implement an integer based power function pow(int, int)
The naive algorithm looks pretty simple actually:
rad = 1
i = 1
While n > 1:
increase i
if i divides n:
multiply rad by i
while i divides n, divide n by i
You're already performing all these steps, the fact is you're doing much more than that (and using much more space), so why not just get rid of unnecessary actions?
define your own int powi(int,int) function and check if the error petsists
Related
Here is the link to the question. Essentially, it asks to find the kth number having digit sum as 10. I have tried multiple solutions and also looked upon solutions online. Specifically this one (also shared below). The one with constant time talks about outliers in Arithmetic Progression and uses it to find the nth number having sum as 10. Obviously, the code is incorrect as it fails for test cases when k=1000 etc.
#include <bits/stdc++.h>
using namespace std;
int findNth(int n)
{
int nthElement = 19 + (n - 1) * 9;
int outliersCount = (int)log10(nthElement) - 1;
// find the nth perfect number
nthElement += 9 * outliersCount;
return nthElement;
}
int main()
{
cout << findNth(5) << endl;
return 0;
}
Eventually, I ended up writing combination of Arithmetic Progression + brute force as below
#include <bits/stdc++.h>
using namespace std;
#define ll unsigned long long
int main() {
int n;
cin >> n;
int count = 0;
ll i = 19;
for (; ; i += 9) {
int curr = i;
int localSum = 0;
while (curr) {
localSum += curr%10;
curr /= 10;
}
if (localSum == 10) {
count += 1;
}
if (count == n) {
break;
}
}
cout << i << endl;
return 0;
}
I am wondering, if there is no constant time or better algorithm that does not require me to calculate the sum, but my algorithm always hops in a way that I have number whose digit sum is 10?
Here is a Python solution that you can translate into C++.
cached_count_ds_l = {}
def count_digit_sum_length (s, l):
k = (s, l)
if k not in cached_count_ds_l:
if l < 2:
if s == 0:
return 1
elif l == 1 and s < 10:
return 1
else:
return 0
else:
ans = 0
for i in range(min(10, s+1)):
ans += count_digit_sum_length(s-i, l-1)
cached_count_ds_l[k] = ans
return cached_count_ds_l[k]
def nth_of_sum (s, n):
l = 0
while count_digit_sum_length(s, l) < n:
l += 1
digits = []
while 0 < l:
for i in range(10):
if count_digit_sum_length(s-i, l-1) < n:
n -= count_digit_sum_length(s-i, l-1)
else:
digits.append(str(i))
s -= i
l -= 1
break
return int("".join(digits))
print(nth_of_sum(10, 1000))
The idea is to use dynamic programming to find how many numbers there are of a given maximum length with a given digit sum. And then to use that to cross off whole blocks of numbers on the way to finding the right one.
The main logic goes like this:
0 numbers of length 0 sum to 10
- need longer
0 numbers of length 1 sum to 10
- need longer
9 numbers of length 2 sum to 10
- need longer
63 numbers of length 3 sum to 10
- need longer
282 numbers of length 4 sum to 10
- need longer
996 numbers of length 5 sum to 10
- need longer
2997 numbers of length 6 sum to 10
- answer has length 6
Looking for 1000th number of length 6 that sums to 10
- 996 with a leading 0 sum to 10
- Need the 4th past 99999
- 715 with a leading 1 sum to 10
- Have a leading 1
Looking for 4th number of length 5 that sums to 9
- 495 with a leading 0 sum to 9
- Have a leading 10
Looking for 4th number of length 4 that sums to 9
- 220 with a leading 0 sum to 9
- Have a leading 100
Looking for 4th number of length 3 that sums to 9
- 55 with a leading 0 sum to 9
- Have a leading 1000
Looking for 4th number of length 2 that sums to 9
- 1 with a leading 0 sum to 9
- Need the 3rd past 9
- 1 with a leading 1 sum to 9
- Need the 2nd past 19
- 1 with a leading 2 sum to 9
- Need the 1st past 29
- 1 with a leading 3 sum to 9
- Have a leading 10003
Looking for 1st number of length 1 that sums to 6
- 0 with a leading 0 sum to 6
- Need the 1st past 0
- 0 with a leading 1 sum to 6
- Need the 1st past 1
- 0 with a leading 2 sum to 6
- Need the 1st past 2
- 0 with a leading 3 sum to 6
- Need the 1st past 3
- 0 with a leading 4 sum to 6
- Need the 1st past 4
- 0 with a leading 5 sum to 6
- Need the 1st past 5
- 1 with a leading 6 sum to 6
- Have a leading 100036
And it finishes in a fraction of a second.
Incidentally the million'th is 20111220000010, the billionth is 10111000000002000000010000002100, and the trillionth is 10000000100000100000100000000000001000000000000100000000010110001000.
Let n=5, then for matrix
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
then sum of diagonal elements is:
=1+17+25+21+9+5+19+23+13
Sum for n=15?
One way is to make the spiral matrix and then by a loop, we get the answer, but its time and space complexity is large.
Like this https://www.geeksforgeeks.org/sum-diagonals-spiral-odd-order-square-matrix/
but the problem here starts 1 from the center.
Consider the outer "shell" of the matrix. The sum of the values at the four vertices, given a size of n (5 in your example) and a starting value of s (1 in your example) is
s + (s + (n-1)) + (s + (n-1)*2) + (s + (n-1)*3) = 4*s + (n - 1)*6
The same applies to the inner values, once updated n and s:
s = s + 4 * (n - 1)
n = n - 2
If n becomes less then 2, well, either we have the central element or nothing (n is even).
Based on Bob_'s answer, Here is a recursive code in CPP as requested by OP
int shellcalc(int n,int s){
if(n==1)
return s;
else if(n==0)
return 0;
else
{
int sum=4*s+(n-1)*6;
int snew=s+4*(n-1);
int nnew=n-2;
return sum+shellcalc(nnew,snew);
}
}
try it out here https://rextester.com/FLJD46264
Python - https://rextester.com/MDMV32855
I'm pretty new to c++, and at the moment i am trying to make a calculator that calculates a Euklid's Algorithm.
Anyways, what i need help with is how i can add the final number to some kind of array for each loop.
Lets for example say i put in the numbers 1128 and 16. my program will then give this output
1128 % 16 = 70 + 8
70 % 16 = 4 + 6
4 % 16 = 0 + 4
theese three lines is printed, one at the time, for each loop. What i want is to add the last numbers (8, 6 and 4) to an array. How would i do this?
Use Vector instead of array, Hope this Helps!
#include <iostream>
#include<vector>
using namespace std;
int main()
{
int a=1128,b=16,i;
vector<int>arr;
while(a>b)
{
cout<<a/b<<" "<<a%b<<endl;
arr.push_back(a%b);
a/=b;
}
cout<<a/b<<" "<<a%b<<endl;
arr.push_back(a%b); // Case: When a<=b in Vector
for(i=0;i<arr.size();i++)
cout<<arr[i]<<" "; // Array i.e 8 6 4
return 0;
}
Output:
70 8
4 6
0 4
8 6 4 // Array
Is there efficient way to downscale number of elements in array by decimal factor?
I want to downsize elements from one array by certain factor.
Example:
If I have 10 elements and need to scale down by factor 2.
1 2 3 4 5 6 7 8 9 10
scaled to
1.5 3.5 5.5 7.5 9.5
Grouping 2 by 2 and use arithmetic mean.
My problem is what if I need to downsize array with 10 elements to 6 elements? In theory I should group 1.6 elements and find their arithmetic mean, but how to do that?
Before suggesting a solution, let's define "downsize" in a more formal way. I would suggest this definition:
Downsizing starts with an array a[N] and produces an array b[M] such that the following is true:
M <= N - otherwise it would be upsizing, not downsizing
SUM(b) = (M/N) * SUM(a) - The sum is reduced proportionally to the number of elements
Elements of a participate in computation of b in the order of their occurrence in a
Let's consider your example of downsizing 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 to six elements. The total for your array is 55, so the total for the new array would be (6/10)*55 = 33. We can achieve this total in two steps:
Walk the array a totaling its elements until we've reached the integer part of N/M fraction (it must be an improper fraction by rule 1 above)
Let's say that a[i] was the last element of a that we could take as a whole in the current iteration. Take the fraction of a[i+1] equal to the fractional part of N/M
Continue to the next number starting with the remaining fraction of a[i+1]
Once you are done, your array b would contain M numbers totaling to SUM(a). Walk the array once more, and scale the result by N/M.
Here is how it works with your example:
b[0] = a[0] + (2/3)*a[1] = 2.33333
b[1] = (1/3)*a[1] + a[2] + (1/3)*a[3] = 5
b[2] = (2/3)*a[3] + a[4] = 7.66666
b[3] = a[5] + (2/3)*a[6] = 10.6666
b[4] = (1/3)*a[6] + a[7] + (1/3)*a[8] = 13.3333
b[5] = (2/3)*a[8] + a[9] = 16
--------
Total = 55
Scaling down by 6/10 produces the final result:
1.4 3 4.6 6.4 8 9.6 (Total = 33)
Here is a simple implementation in C++:
double need = ((double)a.size()) / b.size();
double have = 0;
size_t pos = 0;
for (size_t i = 0 ; i != a.size() ; i++) {
if (need >= have+1) {
b[pos] += a[i];
have++;
} else {
double frac = (need-have); // frac is less than 1 because of the "if" condition
b[pos++] += frac * a[i]; // frac of a[i] goes to current element of b
have = 1 - frac;
b[pos] += have * a[i]; // (1-frac) of a[i] goes to the next position of b
}
}
for (size_t i = 0 ; i != b.size() ; i++) {
b[i] /= need;
}
Demo.
You will need to resort to some form of interpolation, as the number of elements to average isn't integer.
You can consider computing the prefix sum of the array, i.e.
0 1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9 10
yields by summation
0 1 2 3 4 5 6 7 8 9
1 3 6 10 15 21 28 36 45 55
Then perform linear interpolation to get the intermediate values that you are lacking, like at 0*, 10/6, 20/6, 30/5*, 40/6, 50/6, 60/6*. (Those with an asterisk are readily available).
0 1 10/6 2 3 20/6 4 5 6 40/6 7 8 50/6 9
1 3 15/3 6 10 35/3 15 21 28 100/3 36 45 145/3 55
Now you get fractional sums by subtracting values in pairs. The first average is
(15/3-1)/(10/6) = 12/5
I can't think of anything in the C++ library that will crank out something like this, all fully cooked and ready to go.
So you'll have to, pretty much, roll up your sleeves and go to work. At this point, the question of what's the "efficient" way of doing it boils down to its very basics. Which means:
1) Calculate how big the output array should be. Based on the description of the issue, you should be able to make that calculation even before looking at the values in the input array. You know the input array's size(), you can calculate the size() of the destination array.
2) So, you resize() the destination array up front. Now, you no longer need to worry about the time wasted in growing the size of the dynamic output array, incrementally, as you go through the input array, making your calculations.
3) So what's left is the actual work: iterating over the input array, and calculating the downsized values.
auto b=input_array.begin();
auto e=input_array.end();
auto p=output_array.begin();
Don't see many other options here, besides brute force iteration and calculations. Iterate from b to e, getting your samples, calculating each downsized value, and saving the resulting value into *p++.
I'm making a sudoku solver using recursion and I'm having some problems. I put comments next to my vables and functions to try to make this as clear as possible. Does my logic sound like this would work? Everything else in my code works. It's just the solver/recursion it that is not.
bo
boourn
f nt j=0; j < 9; j++)
rowc c=0;
8) //if row is past 8 then the board is done
return true;
for (int < 10; i++)
{
nextr r; //save next row and col
next;
tcol++; /ncrement next col and row
(nextcol >8) {
nextcol =0;
nx
if(ncol==0 && nextrow ==9)
r(0, 0
}
As of your comment reply, Yes:
Code it properly.
Use methods as they should be used and name them properly.
Have InitilalizeBoard fill all zeros.
Have SetBoard put the start values on the board.
Have SolveBoard try to solve it.
It's a good programming habit to have each method do 1 thing, and make it's name clear.
Having said that, I did something similar a couple of years ago. It's much easier, faster, and takes less code to solve the sudoku with brute force approach using for/while loops, than doing it with recursion, or even coding it to be efficient and try to solve it like a human would (remove options that are obviously wrong and so on).
So, depending on your end result, you might want to code it appropriately ... (I did it to refresh my C++, so didn't care doing it the hard, long way first, and then doing it the short way).
The solver function you provided actually views the whole array, and - provided the other functions used in the code work correctly - should actually solve the puzzle. If you substitute cout << " TRUE TRUE "<<endl; for cout << "["<<r<<"]["<<c<<"]: "<<i<<endl;, you'll notice all indexes are checked.
So the problem must be in either one of the 3 functions you didn't provide code for: row_valid, col_valid or panel_valid.
Note: I don't think using recursion in this particular problem is a good idea. You probably should just do it using a for to check and solve the board. That would be both faster and easier.
Edit after first post was updated
The row_valid and col_valid functions aren't quite valid. You can't check the c !=i part, because it'll make all checks false. Change the if statements to this:
if (v == rowcol[i][c] && v!=0)
and
if (v == rowcol[r][j] && v!=0)
also you need to change the solver a bit:
bool sudoku :: solver(int r, int c) {
while( r < 9 && rowcol[r][c] !=0) {
c++;
if ( c>8) {
c=0;
r++;
}
if (r > 8) {
return true;
}
}
for (int i=1; i < 10; i++)
{
int nextrow, nextcol;
if (row_valid (r, c, i)&& col_valid(r, c, i)&& panel_valid(r,c, i)) {
rowcol[r][c]=i;
nextrow=r; //save next row and col
nextcol=c;
nextcol++; //increment next col and row
if (nextcol >8) {
nextcol =0;
nextrow++;
}
if(nextcol==0 && nextrow ==9) {
return true; //then it's done
}
if (solver(nextrow,nextcol)) {
return true;
}
else{
rowcol[r][c]=0;
}
}
}
return false;
}
This seems to work for me and give me an output of:
1 2 3 4 5 6 7 8 9
4 5 6 7 8 9 1 2 3
7 8 9 1 2 3 4 5 6
2 1 4 3 6 5 8 9 7
3 6 5 8 9 7 2 1 4
8 9 7 2 1 4 3 6 5
5 3 1 6 4 2 9 7 8
6 4 2 9 7 8 5 3 1
9 7 8 5 3 1 6 4 2