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;
}
Related
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
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 6 years ago.
Improve this question
Problem statement:
Find the sum of all the multiples of 3 or 5 below N.
Input Format:
First line contains T that denotes the number of test cases. This is followed by T lines, each containing an integer,N.
Constraints :
1 <= T <= 10^5
1 <= N <= 10^9
Output Format:
For each test case, print an integer that denotes the sum of all the multiples of 3 or 5 below N.
This is my code :
#include <stdio.h>
int main() {
long t,i,x;
scanf("%ld",&t);
long y[t];
for(i=0; i<t; i++) {
scanf("%ld",&x);
long j,k,sum= 0;
if(x<=3)
sum= 0;
else if(x<=5)
sum= 3;
else {
for(j=3; j<x; j+=3)
sum= sum + j;
for(j=5; j<x; j+=5)
if(j%3!=0)
sum = sum + j;
}
y[i] = sum;
}
for(i=0; i<t; i++) {
printf("%ld\n",y[i]);
}
return 0;
}
There is a solution with a time complexity of O(T):
Use the formula for sum of integers 1+2+3+...+n = n*(n+1)/2.
Also note that 3+6+9+...+(3*n) = 3*(1+2+3+...+n) = 3*n*(n+1)/2.
Figure out how many numbers divisible by 3 there are. Calculate their sum.
Figure out how many numbers divisible by 5 there are. Calculate their sum.
Figure out how many numbers divisible by 15 (=3*5) there are. Calculate their sum.
Total sum is sum3 + sum5 - sum15. The numbers divisible by both 3 and 5 (hence by 15) are both in sum3 and in sum5, so unless we subtract sum15 they would be counted twice.
Note that the sum will overflow a 32 bit integer, so make sure you use a 64 bit integer type.
You can achieve constant complexity if you employ the Ancient Power of Mathematics.
First figure out how many numbers divisible by three there are.
Compute their sum.
(You do not need any loops for this, you only need elementary arithmetic.)
Repeat for five and fifteen.
The total is a simple expression involving those three sums.
The details left as an exercise for the reader.
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.
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
This program correctly prints whether a number is even or odd ...
#include <stdio.h>
int main(void)
{
int n;
printf("Please enter a number:");
scanf("%d", &n);
if(n % 2 == 0)
printf("%d is even", n);
else
printf("%d is odd",n);
return 0;
}
I don't understand how n % 2 can give a meaningful result when n is less than two. % is the remainder operation, right? If n is less than two, how can you divide it by two at all?
I am unable to understand the logic of n%2==0. If user input a value less than 2. Then how it give us correct answer?
The operator % performs the modulus (or remainder) operation. The remainder of dividing a number by 2 (when that number is less than 2) is the number itself (with the quotient being 0). For example, one divided by two has a quotient of 0 and a remainder of 1 so 1%2 = 1.
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).