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
INPUT
Integers (0 ≤ Integer < 10^1,000,000)
OUTPUT
Find input mod 3 and input mod 11
EXAMPLE
main(){
int num;//problem here i try long long not pass because it not enough.
scanf("%d",&num);
printf("%d ",num%3);
printf("%d",num%11);
}
"Normal types in C can usually only store up to 64 bits, so you'll have to store big numbers in an array, for example, and write mathematical operations yourself. But you shouldn't reinvent the wheel here - you could try the GNU Multiple Precision Arithmetic Library for this purpose."
-AndiDog
Source:Store and work with Big numbers in C
Take advantage that mod 3 and mod 11 can easily be chained one digit at a time.
#include<stdio.h>
#include<ctype.h>
void mod3_11(void) {
int mod3 = 0;
int mod11 = 0;
int ch;
while (isdigit(ch = fgetc(stdin))) {
int digit = ch - '0';
mod3 = (digit + mod3)%3;
mod11 = (digit - mod11 + 11)%11;
}
printf("mod 3 = %d\n", mod3);
printf("mod 11 = %d\n", mod11);
fflush(stdout);
}
int main(void) {
mod3_11();
return 0;
}
This works as each successive digit is processed, code is taking the previous "mod" * 10
// math
mod_n <-- (10*mod_n + digit)%n
mod3 <-- (mod3*10 + digit) % 3
mod3 <-- (mod3*1 + mod3*3*3 + digit) % 3
mod3 <-- (mod3 + 0 + digit) % 3 (mod3*3*3 % 3 is 0)
mod3 <-- (mod3 + digit) % 3
mod11 <-- (mod11*10 + digit) % 11
mod11 <-- (mod11*11 - mod11*1 + digit) % 11
mod11 <-- (mod11*11 - mod11 + digit + 11) % 11 (add 11 to avoid negative numbers)
mod11 <-- (-mod11 + digit + 11) % 11
Why add 11? What's the difference between “mod” and “remainder”?
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 1 year ago.
Improve this question
#include<stdio.h>
int main(){
int a=10,b=3,c=2,d=4,result;
result = a+a*-b/c%d+c*d;
printf("%d",result);
}
How is this program giving 15 as the output.. I did not understand the logic behind the operation.. Can someone please tell me how calculation is done?
int a = 10, b = 3, c = 2, d = 4, result;
result=a+a*-b/c%d+c*d; // original line, with no spaces added
result = a + (a * (-b) / c % d) + (c * d);
result = 10 + (10 * -3 / 2 % 4) + (2 * 4);
result = 10 + (-30 / 2 % 4) + 8;
result = 10 + (-15 % 4) + 8;
result = 10 + (-3) + 8;
result = 15;
Note that the *, / and % operators have higher precedence than + and that those first three operators have equal precedence and left-to-right associativity. Note also that the unary minus operator (as in -b) has higher precedence than multiplication.
So, adding parentheses to highlight the operators' bindings and order-of-evaluation, and a couple of lines to keep track of the intermediate results, we see the following:
#include<stdio.h>
int main(){
int a=10, b=3, c=2, d=4, result;
result = a + ( ( ( (a*(-b)) ) / c ) % d ) + (c*d);
// ^-30 ^-15 ^-3 ^ 8
// ^ a + -3 = 7 ^ 7 + 8 = 15
printf("%d",result);
}
The relevant code:
int a=10,b=3,c=2,d=4,result;
result=a+a*-b/c%d+c*d;
Now processing this as a compiler would, following the rules of operator precedence:
// Unary minus
result = 10 + 10 * (-3) / 2 % 4 + 2 * 4;
// Multiplication, division, and remainder, with left-to-right associativity.
result = 10 + (-30) / 2 % 4 + 8;
result = 10 + (-15) % 4 + 8;
result = 10 + (-3) + 8;
// Addition and subtraction, with left-to-right associativity.
result = 15;
Note that according to the C99 specification, a == (a / b) * b + a % b must hold for the remainder operation with a negative number.
Codewars Question: (Sum of Digits / Digital Root)
Given n, take the sum of the digits of n. If that value has more than one digit, continue reducing in this way until a single-digit number is produced. The input will be a non-negative integer.
Test Cases:
16 --> 1 + 6 = 7
942 --> 9 + 4 + 2 = 15 --> 1 + 5 = 6
132189 --> 1 + 3 + 2 + 1 + 8 + 9 = 24 --> 2 + 4 = 6
493193 --> 4 + 9 + 3 + 1 + 9 + 3 = 29 --> 2 + 9 = 11 --> 1 + 1 = 2
My code:
#include <bits/stdc++.h>
using namespace std;
int singleDigit(int n)
{
int ans;
while (n > 0)
{
int lastDigit = n % 10;
n /= 10;
ans += lastDigit;
}
while (ans > 9)
{
int n1 = ans;
ans = 0;
while (n1 > 0)
{
int lastDigit = n1 % 10;
n1 /= 10;
ans += lastDigit;
}
}
return ans;
}
int main()
{
cout << singleDigit(49319366) << endl;
return 0;
}
Is there a better or optimized way to solve this problem or to reduce time complexity?
This function works for non-negative integers, adapting for negative numbers is straightforward.
int singleDigit(int n)
{
return (n-1) % 9 + 1;
}
It has the following advantages:
no variables to forget to initialise
no loops to commit an off-by-one error
fast
The disadvantages are:
it is not immediately clear how or why it works
For more information on the last bullet point, see:
Direct formulas for the digital root
Modulo operation with negative numbers
Let´s say a number is called happy if there is any combination of addition and subtraction of the digits, so that the result is 42.
Example:
9999993, 999399 and 399999 is happy because 9 + 9 + 9 + 9 + 9 - 3 = 42
3783985861 is also happy because: 3 + 7 + 8 − 3 + 9 + 8 − 5 + 8 + 6 + 1 = 42
My idea:
count how long the given number is
count the combinations: 2^n combinations | n = number length
for loop and check all combinations so that the result is 42
but how?????
do it recursively. I can do it by adding all digits. But how to check all
combinations?
int isHappy(unsigned int aNum){
int count = 0;
while(aNum != 0){
aNum /= 10;
count++;
}
int nTimes = 1;
for(int i=0;i<count;i++){
nTimes = nTimes * 2;
}
for(int i=0;i<nTimes;i++){
????
}
return nTimes;
}
int main(){
printf("%d", isHappy(999993));
return 0;
}
Posts that are certainly homework can benefit with some guiding code, but not too much - difficult to strike that balance.
For each digit, there are 2 ways to go, add the digit or subtract the digit. #Eugene Sh.. This is a classic consideration for a recursive solution. For an n-digit number, expect O(2**n) iterations.
Other approaches may be more efficient.
Avoid hard coding 42
#define HAPPY 42
Make a helper function that passes in the number and the current sum and returns success status.
What should the terminating condition be?
How to do some of the work?
How to try various paths for the rest of the task?
int isHappy_helper(unsigned int aNum, int sum) {
if (aNum == TBD) {
return sum == HAPPY;
}
// Extract one digit from aNum (how about the least significant digit?)
int digit = TBD;
// What is left in aNum once the above digit is removed?
aNum = TBD;
// Try adding and subtracting the digit with the sum
return isHappy_helper(aNum, TBD) || isHappy_helper(aNum, TBD);
}
Call the helper function with a sum of TBD
int isHappy(unsigned int aNum) {
return isHappy_helper(aNum, TBD);
}
Some test code
void isHappy_test(unsigned int aNum) {
printf("%u %d\n", aNum, isHappy(aNum));
}
int main() {
isHappy_test(0);
isHappy_test(1);
isHappy_test(9999993);
isHappy_test(999993);
isHappy_test(999399);
isHappy_test(399999);
isHappy_test(3783985861);
return 0;
}
Expected output
0 0
1 0
9999993 0
999993 1
999399 1
399999 1
3783985861 1
This question already has answers here:
How to generate a random int in C?
(32 answers)
Closed 6 years ago.
Here is my code:
alpha = ((rand() % 12) + 1) * 2 + 1;
I want to generate random odd numbers between 0-25. But except integer 13. How can I fix this? Thank you.
Generates number from 0 to 23. If it's a 13, then store 25 in your variable :
alpha = ((rand() % 11) + 1) * 2 + 1;
if (alpha == 13) alpha = 25;
int universe[] = {1, 3, 5, 7, /* ... omit 13 ... */ 25};
int index = randto(sizeof universe / sizeof *universe);
alpha = universe[index];
Where randto(n) returns a random number from 0 up to and excluding n.
Make a simple loop to retry the operation if alpha gets 13 :
int alpha = 13;
while (alpha == 13)
alpha = ((rand() % 12) + 1) * 2 + 1;
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.
It's really embarrassing!!I just fail to understand the working of the following little program that uses recursion to calculate the powers of a number "a" ("a" raised to a power "b").Kindly explain the logic used behind this function.I don't understand the use of the "x*x" parameter,the n/2 parameter and the "n modulo 2" part.Please dissect it for me.
#include<stdio.h>
int foo(int,int);
int main() {
int a,b;
printf("Enter number a and its power b\n");
scanf("%d%d",&a,&b);
printf("a raised to b is %d", foo(a,b));
return 0;
}
int foo ( int x , int n) {
int val=1;
if(n>0) {
if (n%2 == 1)
val = val *x;
val = val * foo(x*x , n/2);
}
return val;
}
The idea behind this recursion is, that ab = (a2)b/2, and ab = a(a2)(b-1)/2.
Depending on whether b is odd or even (thats the n%2 == 1 part), you choose one of these formulas to ensure b/2 or (b-1)/2 is still an integer. Note here, that the n/2 in your code actually is (n-1)/2 for odd n, since integer division rounds down automatically.
This recursion terminates since the exponent grows smaller with each step.
This makes use of the fact that a power such as x^23 can be rewritten as x^16 * x^4 * x^2 * x^1
Now computing x^16 is fairly easy, because it's just (((x^2)^2)^2)^2 which is only 4 multiplications instead of computing x * x * x * ... 16 times ... * x.
Now notice that while computing x^16 you've also run across x^4 and x^2 which you needed to compute your number. So in the end, you've computed x^23 in only 7 multiplications instead of 22.
Now where the n % 2 and n / 2 come in to the picture is in deciding if the power of 2 is in n (in our example, is 8 in the binary representation of 23? no).
So you just iterate through the bits of n. You square x every time, and if there's a 1 in the current n bit you're looking at, you multiply the squared number into your result.
Update:
The trick to writing a number out this way is to look at n in binary. 23 is 101112, or we can write out the place values 23 = 1*16 + 0*8 + 1*4 + 1*2 + 1*1.
This means x^23 = x^(16 + 4 + 2 + 1) and thanks to the exponential laws, = x^16 * x^4 * x^2 * x^1 which is what we started with.
As another quick example: take x^44. We write it in binary as 1011002 so we can say
44 = 1*32 + 0*16 + 1*8 + 1*4 + 0*2 + 0*1 = 32 + 8 + 4
so
x^44 = x^(32 + 8 + 4) = x^32 * x^8 * x^4
we then calculate the following
1: x^2 = (x)^2 (from the x we are given)
2: x^4 = (x^2)^2 (x^2 from step 1)
3: x^8 = (x^4)^2 (x^4 from step 2)
4: x^16 = (x^8)^2 (x^8 from step 3)
5: x^32 = (x^16)^2 (x^16 from step 4)
6: x^44 = (x^32) * (x^8) * (x^4) (using results of steps 2, 3, and 5)
As you stated, foo works recursively. Why don't you go through it step by step? Assume a==2 and b==3, you get
1st move
int foo ( int x , int n) // x == 2, n==3
{
int val=1;
if(n>0) // n == 3, true!
{
if (n%2 == 1) //true!
val = val *x; // val = 1 * 2;
val = val * foo(x*x , n/2); // next step
}
return val;
}
2nd move
int foo ( int x , int n) // x == 4, n==1
{
int val=1;
if(n>0) // n == 1, true!
{
if (n%2 == 1) //true
val = val *x; val = 1 * 4;
val = val * foo(x*x , n/2); // next step -> 4 * ...
}
return val;
}
In the 2nd step you return 4 which yields in the first step
val = val * foo(x*x , n/2); // 2 * 4 in the first step and this equals 8