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 7 years ago.
Improve this question
I have this question that requires to multiply two large numbers . I thought of adding the first number A , B times ( B is the second number) . I made the algorithm for adding two large numbers . so I thought this would work . the question is , would it take a long time to do this algorithm ? , adding a number to itself a lot of times ?
the question is , would it take a long time to do this algorithm ? , adding a number to itself a lot of times ?
Yes. That's a very slow method of multiplying numbers as you need to to a additions when you add b to itself a times. For better performance and still a reasonably simple algorithm, consider a shift-and-add procedure like this (multiplying a and b, putting the result in q):
q ← 0, i ← 0
if 2i > a then return q
if a & (1 ≪ i) then q ← q + (b ≪ i)
i ← i + 1
goto 2
Fast algorithms for this kind of problem are Karatsuba multiplication, Toom-Cook multiplication and Schönhage-Strassen multiplication.
Check this algorithm:
long long multiply(long long a, long long b)
{
if(a < b)
swap(a, b);
long long c = 0;
for(int i = 0; (1ll << i) <= b; ++i)
{
if(((b >> i) & 1ll) == 1ll)
{
c += a << i;
}
}
return c;
}
It works in logarithmic speed of min(a, b).
If your numbers are really large, Fast Fourier Transform (https://en.wikipedia.org/wiki/Fast_Fourier_transform) and Karatsuba algorithm (https://en.wikipedia.org/wiki/Karatsuba_algorithm) might help you.
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 1 year ago.
Improve this question
while(v!=0)
{
temp=u%v;
u=v;
v=temp;
}
I couldn't understand this equation.Why there is u=v and also v=temp.
how can this equation find greatest common devisor.And what does temp mean?
The algorihm is called "euclidian algorithm" (see Wikipedia).
Let x be the greatest common divisor (gcd) of u and v and u > v.
Then x is also gcd of v and u-v.
In the algorithm, you keep subtracting the smaller number from the larger number until one of them becomes the gcd x.
The temp = u % v means u modulo v (subtracting v from u as often as possible)
So after this step you have smaller numbers temp and v than you started with, that have the same gcd.
The smaller value is now in temp, so temp < v, otherwise you could continue subtracting.
To be able to reuse the code, you have to make sure the larger value is in u and the smaller value is in v, so v becomes your new u and temp becomes you new v.
To break the loop v (temp) has to become 0. To reach 0, u must be a multiple of v before the modulo operation.
The gcd of a number and its multiple is the number itself, so v stored to u in this case.
Since over all the time the gcd x of the numbers did not change, we finally have u == x.
This scheme with temp is commonly used to swap two values.
temp=a;
a=b;
b=temp;
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 2 years ago.
Improve this question
How to calculate the sum of integer divisions 5/1 + 5/2 + 5/3 +5/4 + 5/5 in O(1) using C integer division where 5/2 = 2, 5/3 = 1, 5/4 = 1 and so on...
I can find it using for loop but time complexity is O(n) for that.
You can do it like this:
short sum() {
return 5/1 + 5/2 + 5/3 + 5/4 + 5/5;
}
Since the number of computations is fixed, this will be θ(1). It will probably even be evaluated at compile time, so the actual compiled code will likely look equivalent to this:
short sum() {
return 10;
}
I can find it using for loop but time complexity is O(n) for that.
Actually, it isn't. Since the number of iterations is bounded by a constant, it will still be O(1), even with a loop.
compilers are quite smart :)
int foo(void)
{
int result = 0;
for(int i = 1; i < 6; i++)
result += 5/i;
return result;
}
foo:
mov eax, 10
ret
https://godbolt.org/z/E55Pjh
The best way to learn is to test yourself
This question already has answers here:
How is the square root function implemented? [closed]
(15 answers)
Closed 5 years ago.
I am solving some tasks from school olimpiads, and I got stuck on one question. I found the solution for the task, but my solution requires square rooting. My code works fine for first 12 inputs, but then it gives wrong answers. I guess that it is due to extremely large inputs, which can be as large as 10^400000. So I would like to know if there are ways to calculate whole number parts of square roots of these extremely large inputs in C. Here is the code:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int main(){
long long n;
scanf("%lld", &n);
long long ans;
ans = sqrtl(n-1);
long long result;
result = ans+1-llabs(n-ans*ans-(ans+1));
printf("%lld\n", result);
return 0;
}
In a nutshell, you can roll a long square root algorithm by the dichotomic method as follows:
choose a long number representation (array of unsigned ints);
implement long addition and subtraction (pretty trivial, except for carries);
implement halving (also requires some care for carries);
implement long comparison (similar to subtraction).
[Note that addition allows you to implement doubling and quadrupling, and halving also yields division by four.]
Then set d= 1 and repeatedly double d until d² > N. (Every time you double d, you quadruple d².)
Next, set a= 0 so that the invariant
a² ≤ N < (a + d)²
is established, and repeatedly halve d while keeping the invariant. This is achieved by
d= d/2; if N ≥ (a + d)², set a= a + d; else keep a unchanged.
In the end, you will narrow down to
a² ≤ N < (a + 1)²
so that a is the integer square root.
To evaluate the condition
N < (a + d)² = a² + 2ad + d²,
or
N - a² < 2ad + d²,
it suffices to keep a trace of the terms N - a², 2ad and d² and update them as you modify d or a. This only takes the aforemetioned primitive operations.
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;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
If I have the number 88 how would I add 00 to the end of and turn it into 8800? Bitwise shifts is the only way I can think of to do this but it doesn't work. It completely changes the numbers.
Bitwise shifts can only be used to multiply by powers of two, you simply want multiplication. Just run:
printf("%d", 88 * 100);
to print 8800.
If all you want to do is literally add 00 to the end of numbers you can instead do:
printf("%d00", 88);
You cannot do everything with bitwise shift operators alone. Its mathematically impossible to say it straight. But if you still insist you can do something like (88 << 6) + (88 << 5) + (88 << 2)
As a comment points out your answer can be obtained simply by multiplying your number by hundred.
So, you read from a file, and want to add two zeroes to it. Two ways I can see to do this: String-wise and Numerically.
String-wise, you can use
strcat(inputedText, "00");
(or just printf, or possible other solutions)
numerically, you can convert the inputted data from the file to an int, and multiply it by 100. If you need to print it, Vality's answer shows how to do that.
Live run: http://ideone.com/wiW5Zb
#include <stdio.h>
#include <stdint.h>
int64_t calc(int64_t value)
{
int64_t t = 1;
while(t < value)
t *= 10;
return t;
}
int64_t concatnum(int64_t a, int64_t b)
{
return (a * calc(b)) + b;
}
int main()
{
int a = 1000;
int b = 11;
int c = concatnum(a, b); //b must be greater 10.
printf("%d", c);
return 0;
}