I have to factorize a number given by text in C++, this number is greater than a double, and I can't use external libraries. I know this number is a product of the first 25 prime numbers. Is there a way I can process this number and give it's descomposition in prime?
Whats the best algorithim I could use to do so?
Related
I'm following along a series of youtube lectures on modern C++ by Igor Bogoslavskyi. In CPP-06 he's shown how floating point numbers are represented internally i.e. sign bit, magnitude and mantissa. In the homework he asks to implement the following:
int CountSameSignificantDigits(double a, double b);
This function should count how many significant digits are there between the two numbers. Only count
the number of significat digits up to 100, so that the function returns 100 if the numbers are equal.
It does not make sense to me how he wants the count of up to 100 significant digits. What if the two same doubles have less than 100 significant digits? My current idea is to simply convert these values to strings and iterate over them, but I'm not confident this is a good solution, and even then I'm not sure how to deal with aformentioned ambiguity. Any thoughts?
You can use to_string() function from the <string> header to convert given number and after that, you can access the individual digits using indexing for comparision like:
string a="hello"
Then a[0] is 'h'.
I have a program that calculates large numbers by storing them as strings, so that I can have very large digits that extend beyond long long.
I can add the strings using a function I've written that models how we humans do addition by hand on paper, and it works. I'm able to add large "string integers" together accurately, even when they have hundreds of digits in them.
I now want to use this to enumerate large numbers and test for primality. The problem is, I don't know how I would do this on a string-int that is very large, because I can't convert it to a long long and then perform the test.
Are there techniques for testing for prime numbers that would work on the digits of the number or something? How would I try to factor large numbers that are represented as strings, and test if numbers are factors of it, etc? How do I approach this problem?
so the problem I have is that there is two integers (a, b) that is in [1, 10^16] interval and I need to do find out how many digits will number a^b have? Those numbers are too big for saving them on single variables, and if I write them on Array it would take a lot of time.
Is there a way to count the number a^b number of digits with some kind of formula or any simpler way then Arrays?
after fixing the one-off error suggested in the comments
number of digits of a^b = floor( b * log(a) ) + 1
karakfa has it right.
The base-k logarithm of a number n, rounded up to the nearest whole number, will give you the number of digits required to represent n in base k.
EDIT: as pointed out in comments, it should not be rounded up, but rounded down and then incremented by one. This accounts for round powers of 10 having an extra digit.
If your number is a^b then take the base-10 logarithm, log a^b and use the laws of logarithms to simplify as b log a. Note that this simplification happens inside the ceiling function so the simplification is valid. Computing log a should not be an issue (it will be between 0 and 16) and b is known. Just make sure to round after multiplying, not before.
Note that limited precision of floating-point numbers may introduce some errors into this method. If the true value of b x log a is different from the nearest floating-point representation of b x log a in such a way that they fall on different sides of an integer, the method fails. You can possibly detect when you are close to this condition and remediate it somehow.
You could use a library that supports arbitrarily large numbers, like GMP .
The core C++ language itself offers no types to work with such large numbers. So either you use a pre-existing library or write one yourself (I suggest the former - don't re-invent the wheel).
How to find the number of ways a number n is written with summation of one or more prime numbers?
For example:
n=5, the way is 2. 5,(2+3).
n=10, the way is 4. (2+2+2+2+2),(2+2+3+3),(2+3+5),(5+5).
1<=n<=1000
You might try searching for "prime partitions." Or you might be interested in this post at my blog. I won't give the code because you said you don't want it.
The text in quotes gives a bit of background on my program in case it's needed to understand my issue, you might be able to fully understand with the stuff at the end unquoted if you don't feel like reading it.
I'm working on the common project of sorting in C++, and I am
currently doing radix sort. I have it as a function, taking in a
vector of strings, an integer holding the max number of digits, and an
integer with the radix/base of the numbers: (numbers, maxDigits, radix)
Since the program takes in numbers of different base and as a string,
I'm using stoi to convert them to a base 10 integer to make the
process easier to generalize. Here's a quick summary of the algorithm:
create 10 queues to hold values 0 to 9
iterate through each digit (maxDigit times)
iterate through each number in the vector (here it converts to a base 10)
put them into the queue based on the current digit it's looking at
pull the numbers out of the queues from beginning to end back into the vector
As for the problem I'm trying to wrap my head around, I want to change the maxDigit value (with whatever radix the user inputs) to a maxDigit value after it is converted to base 10. In other words, say the user used the code
radixSort(myVector, 8, 2)
to sort a vector of numbers with the max number of digits 8 and a radix of 2. Since I convert the radix of the number to 10, I'm trying to find an algorithm to also change the maxDigits, if that makes sense.
I've tried thinking about this so much, trying to figure out a simple way through trial and error. If I could get some tips or help in the right direction that would be a great help.
If something is in radix 2 and max digits 8, then its largest value is all ones. And 11111111 = 255, which is (2^8 - 1).
The maximum digits in base 10 will be whatever is needed to represent that largest value. Here we see that to be 3. Which is the base 10 logarithm of 255 (2.40654018043), rounded up to 3.
So basically just round up log10 (radix^maxdigits - 1) to the nearest whole number.