I made a function to double every other digit of a number, but for some reason it converts the in to hex before returning. I checked, and that hex is accurate to an actual number, but how do I get it to stop returning hex? Here's the code.
unsigned long long* Luhn_Algorigthem::double_every_other_value(unsigned long long int in) {
unsigned long long int* out = new unsigned long long;
int counter = 10;
for (int i = 0; i < std::to_string(in).length(); i++) {
if (i % 2 == 0) { //Is even
counter * 10;
out += (unsigned long long)((in % counter) * 2);
}
else { //Is odd
out += (unsigned long long)(std::to_string(in).at(i));
}
}
doubled_val = (unsigned long long)out;
return (unsigned long long*)33;
delete out;
}
Unsigned and long are type modifiers (like adjectives without a noun), have you tried unsigned long int for explicit type casting?
#TheUndedFish helped solve the problem for me, I ended up just getting rid of the pointer, and used the doubled val variable for it instead without allocating memory to the heap. This is the updated code:
unsigned long long int Luhn_Algorigthem::double_every_other_value(unsigned long long int in) {
//unsigned long long int* out = new unsigned long long;
int counter = 10;
for (int i = 0; i < std::to_string(in).length(); i++) {
if (i % 2 == 0) { //Is even
counter * 10;
doubled_val += (in % counter) * 2;
}
else { //Is odd
doubled_val += std::to_string(in).at(i);
}
}
//delete out;
return doubled_val;
}
Related
This is from a dp question and the logic seems to work for unsigned int but shows the following error when using long long int even though long long int is much bigger than unsigned int.
Error: signed integer overflow: 83700400893462717 + 9207044098280898870 cannot be represented in type 'long long'
What is interesting is than the code works fine even for unsigned long long int.
int numDistinct(string s, string t) {
int n = t.length() , m = s.length();
vector<vector<unsigned long long int>> dp(t.size() + 1, vector<unsigned long long int>(s.size() + 1));
dp[n][m] = (long long int)1;
for(int i=n ; i>=0 ; i--){
for(int j=m-1 ; j>=0 ; j--){
if(i == n){
dp[i][j] = (long long int)1;
}else if(t[i] == s[j]){
dp[i][j] = dp[i+1][j+1] + dp[i][j+1];
}else{
dp[i][j] = dp[i][j+1];
}
}
}
return dp[0][0];
}
This question is from leetcode code called Distinct Subsequences
https://leetcode.com/problems/distinct-subsequences/
As pointed in comments unsigned numbers can't overflow, but instead wrap around using the properties of modulo operation.
For instance, when unsigned int is 32 bits, then the result would be: (a + b) mod 2^32.
I wanted to post this on the Arduino forum but couldn't find any "new post" button...
Anyway, I wrote this function to convert a binary string into an int/long.
However, it doesn't always work with big numbers.
The following code is supposed to return "888888" but returns "888.887"
void setup() {
Serial.begin(9600);
String data = "11011001000000111000"; //888888
Serial.print(binStringToInt(data));
}
unsigned long binStringToInt(String bin) {
unsigned long total = 0;
int binIndex = 0;
for (int i = bin.length() - 1; i > - 1; i--) {
total += round(pow(2, binIndex)) * (bin.charAt(i) - '0');
binIndex++;
}
return total;
}
You can use a simpler function to achieve that:
long binary_to_int(char *binary_string){
long total = 0;
while (*binary_string)
{
total *= 2;
if (*binary_string++ == '1') total += 1;
}
return total;
}
void setup(){
Serial.begin(9600);
String data = "11011001000000111000";
Serial.print(binary_to_int(data.c_str())); // prints 888888
}
I used.c_str() to get a char * to Arduino String.
When programming Arduino rather forget about String and vectors, use C strings and C arrays as it is very resources limited.
Never use floats and float functions when dealing with integers.
unsigned long long convert(const char *str)
{
unsigned long long result = 0;
while(*str)
{
result <<= 1;
result += *str++ == '1' ? 1 : 0;
}
return result;
}
How to find count of numbers between a and b (inclusive) which contains 0 as their digit. I am not able to get this with the idea which i have used below in my code, it becomes very complex in case of leading zeroes
for ex if a=1 and b=200 total number should be 29 but i am getting 39.
Can anyone suggest an efficient way of doing it?
constraints:
1<=a<=b<=10^17
code:
long long int F(long long int dig, long long int a, long long int num)
{
if(dig == 0) {
if(a>0)
return 1;
else
return 0;
}
if(mem[dig][a])
return D[dig][a];
mem[dig][a] = 1;
long long int ret = 0;
for(long long int i = 0;i<=9;i++) {
if(i==num)
ret=(ret+F(dig-1,a+1,num));
else
ret=(ret+F(dig-1,a,num));
}
D[dig][a] = ret;
return ret;
}
long long int solve(long long int x,long long int num)
{
char cad[100];
long long int ret = 0;
long long int a=0,b=0,c=0,j;
sprintf(cad,"%lld",x);
int len = strlen(cad);
long long int qued = len;
for(long long int i = 0;i < len;i++) {
qued--;
long long int d = cad[i] - '0';
for(j=0;j <d;j++) {
if(j==num) {
if(num==0 && i==0)
a=a+0;
else
a=a+1;
ret=(ret+F(qued,a,num));
}
else
ret=(ret+F(qued,a,num));
}
if(d==num)
a=a+1;
}
return ret;
}
solution is -> solve(b+1,0)-solve(a,0)
but i am getting wrong answer with this
the link from which i got the above idea is http://codeforces.com/blog/entry/8221
A Simple way to do it would be:
inList = [1, 2, ... n]
outList = []
for i in inList:
for j in len(i):
if i%10 || i==0:
//there is a 0
outList.add(i)
break;
if(i<10)
//i cannot contain a 0
break;
i=i/10
Probibly only works with positive ints, but its trivial to account for negitive numbers.
Simple and efficient O(10N) or O(17N) according to your constraints
How can we store and print factorial(2^n) / (2^n -1))mod1000000009 in C++.Here n can be as large as 20. When I try to print this using the following code, it shows segmentation fault for n=20
#include
#include
using namespace std;
long int factorial(int n)
{
if(n<=1){return 1;}
else
return (n%1000000009)*(factorial(n-1))%1000000009;
}
int main()
{
int K;
long long int numofmatches=0;
long long int denominator=0;
long long int factor=0;
long long int times=0;
long long int players=0;
cin>>K;
if(K==1)
{
cout<<2<<endl<<2<<endl;
return 0;
}
else
{
denominator=pow(2,K);
cout<<"Denominator="<<denominator<<endl;
numofmatches=factorial(denominator)%1000000009;
denominator-=1;
cout<<"numberofmatches="<<numofmatches<<endl;
cout<<"Denominator="<<denominator<<endl;
factor=numofmatches/denominator;
cout<<"Factor="<<factor<<endl;
while(times<=denominator)
{
cout<<(times*factor)<<endl;
++times;
}
}
return 0;
}
First of all, note that (2^n)! / (2^n-1) is equal to (2^n-2)! x 2^n.
Now, (2^20-2)! by itself is already an extremely large number to calculate.
What you can do instead, is to modulo the intermediate result with 1000000009 after every multiplication:
#define MAX ((1<<20)-2)
unsigned long long res = 1;
for (unsigned int i=1; i<=MAX; i++)
res = (res*i)%1000000009;
res = (res*(MAX+2))%1000000009;
If you want to iterate all values of n between 1 and 20, then you can use:
#define MAX_N 20
unsigned int arr[MAX_N+1] = {0};
void Func()
{
unsigned int i = 1;
unsigned long long res = 1;
for (int n=1; n<=MAX_N; n++)
{
unsigned int max = (1<<n)-2;
for (; i<=max; i++)
res = (res*i)%1000000009;
arr[n] = (unsigned int)((res*(max+2))%1000000009);
}
}
BTW, for any n larger than 29 the result will simply be 0, as (2^30-2) is larger than 1000000009.
So (2^30-2)! is divisible by 1000000009, and therefore, (2^30-2)! mod 1000000009 equals 0.
When the program runs, it crashes at long long thisLong = atoll(c); Is there any reason for this?
string ConvertToBaseTen(long long base4) {
stringstream s;
s << base4;
string tempBase4;
s >> tempBase4;
s.clear();
string tempBase10;
long long total = 0;
for (signed int x = 0; x < tempBase4.length(); x++) {
const char* c = (const char*)tempBase4[x];
long long thisLong = atoll(c);
total += (pow(thisLong, x));
}
s << total;
s >> tempBase10;
return tempBase10;
}
atoll needs const char* as input, but tempBase4[x] only returns char.
If you want to convert each character in string to decimal, try:
for (signed int x = 0; x < tempBase4.length(); x++) {
int value = tempBase4[i] -'0';
total += (pow(value , x));
}
Or if you want to convert the whole tempBase to long long:
long long thisLong = atoll(tempBase4.c_str());
total += (pow(thisLong, x));