Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to append any static single digit in given number using bitwise operation.
Let the static number is 1 ,
If the number is
2 => 12
31 => 131
24 => 124
11 => 111
Is it possible to do..?
Here why I am strict with bitwise means , I want to maintain as integer values.
If your dbms supports basic math functions (Oracle for instance), you can use:
SELECT NUMBER + Power(10, Floor(Log(10, NUMBER)) + 1)
FROM TABLE;
If not you could get away with a string trick like:
SELECT TO_NUMBER('1' || TO_CHAR(NUMBER))
FROM TABLE;
(using Oracle)
Bitwise operators won't work that well on decimal numbers since powers of two and powers of ten don't mesh well (I have no idea what the database tag is for here, this seems to be totally unrelated).
If you want a function to add a 1 to the left of an arbitrary number as you seem to indicate, you can use the following algorithm:
def prefixWithOne (n):
if n == 0:
return 10
addNum = 1
testNum = n
while testNum > 0:
testNum = int (testNum / 10)
addNum = addNum * 10
return addNum + n
For example, the following C code will do that:
unsigned int prefixWithOne (unsigned int n) {
unsigned int testNum = n;
unsigned int addNum = 1;
if (n == 0) return 10;
while (testNum > 0) {
testNum /= 10;
addNum *= 10;
}
return addNum + n;
}
(with the usual caveats about watching out for overflow).
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm trying to write a C program to convert a number given a base, to any other base (Eg. base 2 binary number to base 8, or base 3 to base 16 hex). I've researched this quite a bit, and already read through a similar article, The math behind converting from any base to any base without going through base 10?] but in the explanation given, they utilize an array, which I am not allowed to do for this program.
Without writing a lengthy program with a method for each possible combination of base conversion, I don't see how this is possible without an array to store possible values. I do understand that there is a change of base formula with logs that allows me to change between number bases, but I am unclear how I would apply this, as this formula only gives a decimal number answer, which I would still need to convert.
int log_base_n(int n, int logof)
{
double logBaseN = log10((double) logof) / log10((double) n);
return (int) ceil(logBaseN);
}
Here is my binary to decimal conversion which I am trying to use as an intermediate step:
/**
* Convert decimal numbers to binary. Uses a greedy subtraction
* algorithm. Assumes max integer allowed is 2 to 16 power.
*
* #param numberToConvert
*/
void decToBin(int numberToConvert)
{
int power = 16;
double ans = pow(2, power);
if (numberToConvert > ans)
{
printf("ERROR: Number too large to convert!\n");
return;
}
while (ans > numberToConvert)
{
power--;
ans = pow(2, power);
}
printf("%d", 0);
int i = power;
while (i >= 0)
{
ans = pow(2, i);
numberToConvert = numberToConvert - ans;
printf("%d", 1);
i--;
while ((pow(2, i) > numberToConvert) && (i >= 0))
{
printf("%d", 0);
i--;
ans = pow(2, i);
}
}
}
I know Java has a parseInt() method, that does base conversions, but is there something similar I can implement in C without having to write methods for each possible conversion like the one above, while still utilizing a logarithm related idea? Any help would be greatly appreciated.
but is there something similar I can implement in C without having to write methods for each possible conversion like the one above, while still utilizing a logarithm related idea?
Logarithm is a poor choice. The computation of logs in code is not exactly the same as their mathematical counterpart and leads to incorrect output.
The below is a problem should the quotient result in a value just a little higher than a whole number expected value. Of course, log10() is a problem for logof <= 0.
double logBaseN = log10((double) logof) / log10((double) n);
return (int) ceil(logBaseN);
Further, the calculation of log_base_n() is quite unnecessary.
This is an integer problem. Use integer math.
A simply non-array solution "to convert from any base to another base"1 is to use recursion.
void print_int_base(int numberToConvert, int base) {
// For now, assume numberToConvert >= 0, 2 <= base <= 36
if (numberToConvert >= base) {
print_int_base(numberToConvert/base, base);
}
int digit = numberToConvert%base;
int c = digit < 10 ? digit + '0' : digit + 'A';
putchar(c);
}
Test code
#include <stdio.h>
void print_int_base_test(int numberToConvert, int base) {
printf("%10d %2d:", numberToConvert, base);
print_int_base(numberToConvert, base);
puts("");
}
int main() {
int numberToConvert = 42;
for (int base=2; base<=20; base++) {
print_int_base_test(numberToConvert, base);
}
}
Output
42 2:101010
42 3:1120
42 4:222
42 5:132
42 6:110
42 7:60
42 8:52
42 9:46
42 10:42
42 11:39
42 12:36
42 13:33
42 14:30
42 15:2M
42 16:2K
42 17:28
42 18:26
42 19:24
42 20:22
1 OP's idea of conversion apparently is to print the int in various bases.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Is there an option to store a hex complex number in c?
The remainder of a number divided by 3 equals to the sum of its digits modulo 3.
Once you calculate the remainders for the two numbers (not need to represent each number's value), sum those. If result modulo 3 is zero, the sum of the number is a multiplication of 3.
Well I guess you are not getting the problem. Rather getting the input is easier but processing it is not.
So no type would be big enough to accurately hold the value - these are large. Why not store it as string?
You can store it as a char array and use fgets for that (this is only if you want to print the number otherwise not needed). You can use getchar() also and do the sum as shown in the proof here.
After doing it, just do one thing - check each digit-char and then calculate it's sum mod 3. That way you will get the value of the result and keep it adding. (The resultant mod sum tells you about the divisibility). That is what you want exactly.
What I meant is?
(A + B) mod 3
= ( A(n)A(n-1)A(n-2)...A(1)A(0)
+ B(m)B(m-1)B(m-2)...B(1)B(0) ) mod 3
= ( [ A(n) + A(n-1) + A(n-2) + ... + A(1) + A(0) ] mod 3
+ [ B(m) + B(m-1) + B(m-2) + ... + B(1) + B(0) ] mod 3 ) mod 3
Rules:
if a≡b (mod m) and c≡d (mod m) then
a+c ≡ b+d (mod m) and
ac ≡ bd (mod m)
Example code
#include <stdio.h>
int main(void){
int c,sum = 0;
while(isdigit(c = getchar()))
sum+=(c-'0'),sum%=3;
while(isdigit(c = getchar()))
sum+=(c-'0'),sum%=3;
printf("%s\n", sum?"Non-divisible":"divisible");
return 0;
}
This question already has answers here:
How to concatenate two integers in C
(10 answers)
Closed 5 years ago.
Currently I am having a speed bump with adding two different integers. For example
int i = 32;
int j = 50;
/* Add i and j together into 3250 */
What I thought was changing the integers into strings and add them together but that takes too much effort. Is there any other way?
The solution in the decimal system is:
int result = 100* i + j;
In case this should be generic you'll need the following algorithm:
int shift = 10;
while(j >= shift) {
pow *= 10;
}
int result = i * pow + j;
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
********** for n<=10**********
Some output may even exceed the range of 64 bit integer.
how to find sum of series defined by the following relation
T(n + 2) = (T(n + 1))2 + T(n)
where T(0) = 0 and T(1) = 1
*yes it is t(n+1) square,,,my issue is how to store big numbers,,like calculate T(12) and try to store the result, it's result is beyond the range of long long int.
For use with gcc on 64-bit machines, if 128-bit are enough:
typedef unsigned int uint128_t __attribute__((mode(TI)));
uint128_t T (int nr) {
uint128_t t0=0,t1=1,t2,sum = 0;
while (nr--) {
t2 = t1*t1 + t0;
t0 = t1;
t1 = t2;
sum += t2;
}
return sum;
}
Recursive solution:
some_big_int_type T(int N)
{
if (N == 0)
return 0;
if (N == 1)
return 1;
some_big_int_type Tpow2 = T(N - 1);
return Tpow2 * Tpow2 + T(N - 2);
}
big_type_int may be uint128_t as Ctx answer.
You need a library that supports arbitrary precision numbers, like GMP for example: https://gmplib.org/
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
What does rand() do in C? I don't use C++, just C.
Visual Studio 2012 tells me that its return type is int __cdecl
And it is part of stdlib.h
It does not take any parameters.
How can I set the range in which it generates (pseudo)random numbers?
Your answers are greatly appreciated
Now that there's an actual question: You can't. The range is fixed, and it's defined by the constant RAND_MAX if you need to know it (it's [0 .. RAND_MAX])
If you want a different range, you have to arrange that yourself, typically using the modulo operator % and optionally an offset ... for random numbers between 5 and 9 use
int foo = rand() % 5 + 5;
I use a helper function like this in the game i recently wrote:
int
randomNum(int min, int max)
{
static int seeded = 0;
if (!seeded)
{
seeded = 1;
srand((unsigned int)time(0));
}
return (rand() % (max-min+1)) + min;
}
Depends on the compiler. This wiki article contains a list of them: linear congruential generator
If you need to extend the range of rand(), call it multiple times and merge the outputs:
unsigned int r;
/* ... */
r = ((rand()>>4) & 0xff)<< 0;
r += ((rand()>>4) & 0xff)<< 8;
r += ((rand()>>4) & 0xff)<<16;
r += ((rand()>>4) & 0xff)<<24;
Example program to show how rand works by comparing the returned value. This works with Microsoft compiles (no mismatch).
#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char* argv[])
{
unsigned int seed = 1;
unsigned int rand1, rand2;
unsigned int i;
for(i = 0; i < 20; i++){
seed = seed*214013 + 2531011;
rand1 = (seed >> 16) & 0x7fffu;
rand2 = rand();
if(rand1 != rand2)
printf("mismatch %d %d\n", rand1, rand2);
}
return(0);
}
Since only 15 bits of the seed are returned by this version of rand(), then RAND_MAX would be 32767 or hex 0x7fff. As noted in the wiki article, the period is 2^32, this means that seed will cycle through all 4,294,967,296 possible 32 bit values, never repeating until the 4,294,967,296 call to rand(), where seed will have cycled back to 1.