Base 10 to base n conversions [closed] - c++

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I'm trying to write a C++ program that does base-conversions.
I want to convert a decimal number to all the other integer bases from 2 to 20.
Is there an efficient and easy-to-implement algorithm for base conversions?

I don't understand where exactly is the problem? It's very easy and straigtforward to do base conversion: you do it as you would by hand.
divide the number by base
write down the remainder
repeat the process with the integer part of the division
stop when you reach zero
the remainders in reverse order give you the digits in base
Example:
1025 (decimal) to base 15:
1025 / 15 = 68 , remainder 5
68 / 15 = 4 , remainder 8
4 / 15 = 0 , remainder 4
The number in base 15 is 485

You may have two problems:
Parsing from the original base to the computer's native integer representation (strtol is quite good at this).
Formatting into the new base. (itoa is quite good at this).
If you want to write it yourself, you might like the div function. You feed in the number and the base, and it splits off the rightmost digit. Repeat to get all digits.
If you want to be more efficient, you can divide by the base squared, and get two digits at a time (use a lookup table to get the ASCII characters for both digits). Here's an example of some very efficient implementations. Changing it to use a different base would not be difficult.

Related

calculate decimal fraction and show digits after decimal in a precise manner [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
Given a fraction (103993/33102), I need to find 50000 digits after the decimal point for this fraction.
Initially I used setprecision(k) in C++, but it gives only 17 digits after decimal point. I also tried
sprintf (str, "%.500000f", num)
but the result is the same.
I need an algorithm that can solve this and which does not round off the digits after the decimal point i.e., it should be precise.
The best way to work out how to solve a problem like this is to take a ridiculously easy problem and work out the algorithm on that. That way, you won't get confused or lose your place, and the algorithm is the same regardless of the problem. So let's take 4/3.
4 goes into 3 once. We have 1 left over. We output the 1. for the one time it went in. We keep the 1 left over.
We multiply the 1 left over by 10 to get 10. 3 (our denominator) goes into 10 three times. With 1 left over.
We output the 3 because it went in three times. We still have 1 left over.
We go to step 2 and repeat as many times as we need to.
This same algorithm works just this simply regardless of the numerator and denominator, so long as they are positive integers.
Float or double will not do it because they are not precise enough. Others have suggested to use a bignum library. This can be done, but there is another way that works directly with integers.
A technique called modular exponentiation can be used to solve this problem. This lets you calculate all digits without ever running into precision problems.
Fortunately an answer how to do this has already been written:
Getting a specific digit from a ratio expansion in any base (nth digit of x/y)

std::numeric_limits<double>::digits10 = number of digits on the right or left to the radix point? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
http://en.wikipedia.org/wiki/Double-precision_floating-point_format say that double can handle 16 digits of precision to the right part of the number, is digits10 show this number ?
std::numeric_limits<T>::digit10 is the number of decimal digits you can get back when converting a string with a decimal value to T and back to a string again. The count starts at the most significant non-zero digit, independent of where the decimal point is located (as long as you don't conflict with the range restrictions of T, of course). That is, leading and trailing zeros don't matter.
Actually it's just the number of meaningful digits. Point. You can have 12345678901234.5 or 0.0000123456789012345.

c++ array min max range fraction [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have an array which fluctuates about between 0.1429 and 0.1428 it doesn't seem to have a real top or bottom though so those numbers could vary.
if(myarray[N-1]<myarry[N]){/*always happens*/}
if(myarray[N-1]>myarry[N]){/*never happens*/}
the numbers are fractional so there must be smaller fraction in the numbers to show curves on my chart eg: 0.14285216
I am having real trouble with 'greater than' 'smaller than' < > I think it's because i've not got numbers bigger than 1 (myarray[N-1] shows 0 always)
can I do something to my data like increase the range or use another method to '<>'?
really stuck
I'm guessing that what you want to do is display the numbers in an array so as to see the differences between them? The reason for your always/never situation is that the array is sorted, which is probably a good thing. Anyway, to display a greater number of digits, you can use format specifiers, such as
printf ("my ith number: %.10f", myarray[i]);
This will give you myarray[i] with 10 decimal places.
what is:
myarray versus myarry (missing the a between the second r and the y)
Direct from your code given above:
if(myarray[N-1]<myarry[N]){/*always happens*/}
if(myarray[N-1]>myarry[N]){/*never happens*/}
Shouldnt it be:
if(myarray[N-1]<myarray[N]){/*always happens*/}
if(myarray[N-1]>myarray[N]){/*never happens*/}
Also I hope you arent ever using N = 0 as an input to this set of if statements.
You might want to multiply all numbers by 7 and subtract 1 - that will make the relative differences larger.

Generate all Binary Combinations C++ [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I want to write a program that you give an input (number of digits n) and you get as an output a vector of all possible binary grey code generated.
For example,
if n=2, results should be:
V= {00, 01, 10,11}
Actually I am interested in having them bit by bit and not whole integer. Meaning I want to have a 2 D array of integers where I have each word in rows and bits (as int) in cols
Hint: Gray code of num is (num>>1) ^ num. Go through all numbers of the 0..2^N-1 range, and compute their Gray code representation using this simple formula.
EDIT Another hint: the simplest way to convert an integer to binary is using bitset:
bitset<N>((num>>1) ^ num).to_string()
The set V equals { 0, ..., 2^n-1 }. Just compute this set.

Given integer n decide if it is possible to represent it as a sum of two squares of integers [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
INPUT SPECIFICATION:
First line of input contains one integer t <= 10000: number of test cases.
T lines follow, each of them consisting of exactly one integer 0 <= n <= 10^8.
OUTPUT SPECIFICATION:
For each test case output Yes if it is possible to represent given number as a sum of two squares and No if it is not possible.
Hint: A number N is expressible as a sum of 2 squares iff in the prime factorization of N, every prime of the form (4k+3) occurs an even number of times!