Can you help me? When I execute this statement, it keeps saying out of upper bound even if it isn't. What is the problem? Thanks.
if(number_o_1 <= higher)
{
printf("Random number has reached upper bound.\n");
}
else
{
printf("Number 1: %d\n", number_o_1);
number_o_up=number_o_1+n_multiple*(x-1);
while(number_o_up<=higher)
for(x=2;x<number_r;x++)
printf("Number %d: %d\n", x,number_o_up);
}
number_o_1<= higher
Sure you want to print that the number has reached the upper bound when it's lesser than or equal to your upper bound?
It is little hard to figure the logic you would like to implement, but please see the example with exact values and comments to hit the else block.
#include <stdio.h>
int x = 2;
int higher = 100;
int number_o_1 = 10;
int number_r = 4;
int number_o_up = 1;
int n_multiple = 2;
int main()
{
if(number_o_1 >= higher) // probably want >= here to hit else
{
printf("Random number has reached upper bound.\n");
}
else
{
printf("Number 1: %d\n", number_o_1);
number_o_up = number_o_1 + n_multiple * (x-1); // number_o_up = 12
while(number_o_up <= higher) // 12 <= 100
{
for(x = 2; x < number_r; x++) // cond. 2 < 4
{
printf("Number %d: %d\n", x, number_o_up);
}
number_o_up++; //needs to change higher or number_o_up to get out of while
}
}
}
Output:
sh-4.3$ main
Number 1: 10
Number 2: 12
Number 3: 12
Number 2: 13
Number 3: 13
Number 2: 14
...
Number 3: 100
Related
I am writing a program to see if a user entered number is Armstrong or not, here is my code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(){
int x = 0;
printf("Enter a natural number: ");
scanf("%d", &x);
int ans = x;
// Digit Counter
int counter = 0; //Variable for number of digits in the user entered number
int b = x; //For each time number can be divided by 10 and isnt 0
for (int i = 1; i <= x; i++){ // Then counter variable is incremented by 1
b /= 10;
if (b != 0){
counter += 1;
}
}
++counter;
//Digit Counter
int sum = 0;
// Digit Finder
int D;
for (int j = 1; j <= x; j++){
D = x % 10; //Shows remainder of number (last digit) when divided by 10
sum += pow(D, counter); //Raises Digit found by counter and adds to sum
printf("%d\n", sum);
x /= 10; // Divides user entered number by 10 to get rid of digit found
}
if (sum == ans){
printf("%d is a Armstrong number! :)", ans);
}else
printf("%d is not an Armstrong number :(", ans);
//Digit Finder
return 0;
}
My problem is that the program works fine apart from one point, when the program is given a Armstrong number which does not start with 1 then it behaves normally and indicates if it is an Armstrong number or not, but when i input a Armstrong number which start with 1 then it will print out the Armstrong number but -1.
For example: If i input something such as 371 which is an Armstrong number it will show that it is an Armstrong number. However if i input 1634 it will output 1633 which is 1 less than 1634.
How can i fix this problem?, also by the way could someone comment on my code and tell me if it seems good and professional/efficient because i am a beginner in C and would like someone else's opinion on my code.
How can I fix this problem.
You know the number of iterations you want to make once you have calculated the digit count. So instead of looping till you reach the value of x:
for (int j = 1; j <= x; j++){
use the digit counter instead:
for (int j = 1; j <= counter; j++) {
also by the way could someone comment on my code and tell me if it seems good and professional/efficient because i am a beginner in C and would like someone else's opinion on my code.
There's a number of things you can do to improve your code.
First and foremost, any piece of code should be properly indented and formatted. Right now your code has no indenting, which makes it more difficult to read and it just looks ugly in general. So, always indent your code properly. Use an IDE or a good text editor, it will help you.
Be consistent in your code style. If you are writing
if (some_cond) {
...
}
else
//do this
It is not consistent. Wrap the else in braces as well.
Always check the return value of a function you use, especially for scanf. It will save you from many bugs in the future.
if (scanf("%d", &x) == 1)
//...all OK...
else
// ...EOF or conversion failure...
exit(EXIT_FAILURE);
Your first for loop will iterate x times uselessly. You can stop when you know that you have hit 0:
for (int i = 1; i <= x; i++){ // Then counter variable is incremented by 1
b /= 10;
if (b == 0){
break;
}
counter += 1;
}
C has ++ operator. Use that instead of doing counter += 1
int D; you create this, but don't initialize it. Always initialize your variables as soon as possible
C has const qualifier keyword, which makes a value immutable. This makes your code more readable, as the reader can immediately tell that this value will not change. In your code, you can change ans variable and make it a const int because it never changes:
const int ans = x;
Use more descriptive names for your variables. ans, D don't tell me anything. Use proper names, so that the reader of your code can easily understand your code.
These are some of the things that in my opinion you should do and keep doing to improve your code and coding skills. I am sure there can be more things though. Keep your code readable and as simple as possible.
The condition in this loop
for (int i = 1; i <= x; i++){ // Then counter variable is incremented by 1
b /= 10;
if (b != 0){
counter += 1;
}
}
does not make sense because there will be numerous redundant iterations of the loop.
For example if x is equal to 153 that is contains only 3 digits the loop will iterate exactly 153 times.
Also additional increment of the variable counter after the loop
++counter;
makes the code logically inconsistent.
Instead of the loop you could write at least the following way
int counter = 0;
int b = x;
do
{
++counter;
} while ( b /= 10 );
This loop iterates exactly the number of times equal to the number of digits in a given number.
In this loop
for (int j = 1; j <= x; j++){
D = x % 10; //Shows remainder of number (last digit) when divided by 10
sum += pow(D, counter); //Raises Digit found by counter and adds to sum
printf("%d\n", sum);
x /= 10; // Divides user entered number by 10 to get rid of digit found
}
it seems you did not take into account that the variable x is decreased inside the body of the loop
x /= 10; // Divides user entered number by 10 to get rid of digit found
So the loop can interrupt its iterations too early. In any case the condition of the loop again does not make great sense the same way as the condition of the first loop and only adds a bug.
The type of used variables that store a given number should be unsigned integer type. Otherwise the user can enter a negative number.
You could write a separate function that checks whether a given number is an Armstrong number.
Here you are.
#include <stdio.h>
int is_armstrong( unsigned int x )
{
const unsigned int Base = 10;
size_t n = 0;
unsigned int tmp = x;
do
{
++n;
} while ( tmp /= Base );
unsigned int sum = 0;
tmp = x;
do
{
unsigned int digit = tmp % Base;
unsigned int power = digit;
for ( size_t i = 1; i < n; i++ ) power *= digit;
sum += power;
} while ( ( tmp /= Base ) != 0 && !( x < sum ) );
return tmp == 0 && x == sum;
}
int main(void)
{
unsigned int a[] =
{
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407,
1634, 8208, 9474, 54748, 92727, 93084, 548834
};
const size_t N = sizeof( a ) / sizeof( *a );
for ( size_t i = 0; i < N; i++ )
{
printf( "%u is %san Armstrong number.\n", a[i], is_armstrong( a[i] ) ? "": "not " );
}
return 0;
}
The program output is
0 is an Armstrong number.
1 is an Armstrong number.
2 is an Armstrong number.
3 is an Armstrong number.
4 is an Armstrong number.
5 is an Armstrong number.
6 is an Armstrong number.
7 is an Armstrong number.
8 is an Armstrong number.
9 is an Armstrong number.
153 is an Armstrong number.
370 is an Armstrong number.
371 is an Armstrong number.
407 is an Armstrong number.
1634 is an Armstrong number.
8208 is an Armstrong number.
9474 is an Armstrong number.
54748 is an Armstrong number.
92727 is an Armstrong number.
93084 is an Armstrong number.
548834 is an Armstrong number.
Please remove j++ from 2nd loop for (int j = 1; j <= x; j++)
I tried this:
void armstrong(int x)
{
// count digits
int counter = 0, temp = x, sum = 0;
while(temp != 0)
{
temp = temp/10;
++counter; // Note: pre increment faster
}
// printf("count %d\n",counter);
temp = x;
while(temp != 0)
{
sum += pow(temp % 10, counter);
temp = temp/10;
}
// printf("sum %d\n",sum);
if(x == sum)
{
printf("Armstrong\n");
}
else
{
printf("No Armstrong\n");
}
}
int main(){
armstrong(371);
armstrong(1634);
return 0;
}
Let's take this and add the ability to handle multiple numeric bases while we're at it. Why? BECAUSE WE CAN!!!! :-)
#include <stdio.h>
#include <math.h>
double log_base(int b, double n)
{
return log(n) / log((double)b);
}
int is_armstrong_number(int b, /* base */
int n)
{
int num_digits = trunc(log_base(b, (double)n)) + 1;
int sum = 0;
int remainder = n;
while(remainder > 0)
{
sum = sum + pow(remainder % b, num_digits);
remainder = (int) (remainder / b);
}
return sum == n;
}
int main()
{
printf("All the following are valid Armstrong numbers\n");
printf(" 407 base 10 - result = %d\n", is_armstrong_number(10, 407));
printf(" 0xEA1 base 16 - result = %d\n", is_armstrong_number(16, 0xEA1));
printf(" 371 base 10 - result = %d\n", is_armstrong_number(10, 371));
printf(" 1634 base 10 - result = %d\n", is_armstrong_number(10, 1634));
printf(" 0463 base 8 - result = %d\n", is_armstrong_number(8, 0463));
printf("All the following are NOT valid Armstrong numbers\n");
printf(" 123 base 10 - result = %d\n", is_armstrong_number(10, 123));
printf(" 0x2446 base 16 - result = %d\n", is_armstrong_number(16, 0x2446));
printf(" 022222 base 8 - result = %d\n", is_armstrong_number(8, 022222));
}
At the start of is_armstrong_number we compute the number of digits directly instead of looping through the number. We then loop through the digits of n in base b, summing up the value of the digit raised to the number of digits in the number, for the given numeric base. Once the remainder hits zero we know there are no more digits to compute and we return a flag indicating if the given number is an Armstrong number in the given base.
This is the code for finding the last digit of nth Fibonacci number
#include <stdio.h>
int main() {
long i, j, fib[1000];
fib[0] = 0;
fib[1] = 1;
scanf("%li", &j);
for(i = 2; i != 1000; i++)
{
fib[i] = (fib[i - 2] + fib[i - 1]) % 10;
}
printf("%li", fib[j]);
return 0;
}
It shows segmentation fault. How can I fix it?
The only reason I can see for this not working is that you're inputting a number that is outside of the range 0 <= j <= 999. This is due to the limit on your array variable: long fib[1000].
This can be fixed in one of two ways, depending on what you want:
You can add a check to make sure that the input value j is in range, and ask for another number if it isn't.
You can stop using an array variable, and only use three variables: one to store the current value, and two more to store the two previous values. These are updated as you calculate. A loop is still used with this approach.
#1 is the simplest to implement, as shown here:
while (1)
{
printf("j > ");
scanf(" %li", &j);
if (0 <= j <= 999)
{
break;
}
}
#2 is a bit more complex, but it effectively removes the arbitrary limit that j must be less than 1000 (and changes the limit so that j must be less than LONG_MAX):
// num_cache[0] is the number before the previous number
// num_cache[1] is the previous number to the current number
long num_cache[2] = { 0, 0 };
long current_fib = 1;
for (i = 2; i < j; i++)
{
// Push back the numbers
num_cache[0] = num_cache[1];
num_cache[1] = current_fib;
// Calculate the new number
current_fib = (num_cache[0] + num_cache[1]) % 10;
}
One of those solutions should fix your issue.
It appears that the segmentation fault is occurring due to inadequate checking of input values.
If the input to the program is not a valid number, then the value of j will be unchanged after the call to scanf(). Since this variable is uninitialized, this will result in undefined behaviour when you attempt to access the jth element of the fib[] array.
If the value of j is less than zero or greater than 999, you will be accessing a non-existent member of fib[] when you exit the for() loop. Your code should check that j is valid before continuing.
Here's your code with a few modifications to implement these safeguards and move the "magic number" 1000 to a #defined value.
#include <stdio.h>
#define FIBONACCI_LIMIT 1000L
int main(){
long i, j, fib[FIBONACCI_LIMIT];
fib[0] = 0;
fib[1] = 1;
if (scanf("%li", &j) != 1)
{
fprintf(stderr, "Invalid input\n");
return 1;
}
if (j<0 || j>=FIBONACCI_LIMIT)
{
fprintf(stderr, "Number must be in range 0 <= n < %li\n", FIBONACCI_LIMIT);
return 2;
}
for(i=2; i!=1000; i++)
{
fib[i] = (fib[i-2] + fib[i-1])%10;
}
printf("%li\n", fib[j]);
return 0;
}
The code can be improved by getting rid of the fib[] array altogether, since there is no need to store 1000 values when you only need to calculate one value. Furthermore, the final digits of numbers in the Fibonacci sequence form a repeating pattern of 60 values, so your first step should be to replace j with j % 60. Here is an improved version of the code that will work with any non-negative input capable of fitting into a long integer:
#include <stdio.h>
int main() {
long i, j, t, f0=0, f1=1;
if (scanf("%li", &j) != 1)
{
fprintf(stderr, "Invalid input\n");
return 1;
}
if (j < 0)
{
fprintf(stderr, "Number must be non-negative\n");
return 2;
}
j %= 60;
for (i=0; i<j; i++)
{
t = f0;
f0 = f1;
f1 = (f1 + t) % 10;
}
printf("%li\n", f0);
return 0;
}
You did not show what value of the variable j was entered.
Take into account that the next Fibonacci number is calculated incorrectly in the loop.
If to use the integer type unsigned long long then an object of this type can accommodate only 93 Fibonacci numbers.
The program can look like
#include <stdio.h>
#define N 100
int main(void)
{
while ( 1 )
{
unsigned long long fib[N] = { 0, 1 };
unsigned int n;
printf( "Enter a sequantial number of a fibonacci number (0 - exit): " );
if ( scanf("%u", &n) != 1 || n == 0 ) break;
unsigned int i = 2;
for (; i < N && i <= n && fib[i-1] <= fib[i-2] + fib[i-1]; i++)
{
fib[i] = fib[i - 2] + fib[i - 1];
}
if (n < i)
{
printf("#%u: %llu %llu\n", n, fib[n], fib[n] % 10);
}
else
{
puts("Too big fibonacci number");
}
}
return 0;
}
Its output might look like
Enter a sequantial number of a fibonacci number (0 - exit): 1
#1: 1 1
Enter a sequantial number of a fibonacci number (0 - exit): 2
#2: 1 1
Enter a sequantial number of a fibonacci number (0 - exit): 3
#3: 2 2
Enter a sequantial number of a fibonacci number (0 - exit): 93
#93: 12200160415121876738 8
Enter a sequantial number of a fibonacci number (0 - exit): 94
Too big fibonacci number
Enter a sequantial number of a fibonacci number (0 - exit): 0
So, I was working out some exercises from a recommended beginner's book: C Programming: A Modern Approach (2nd Edition) While working out one of the questions I found myself unable to proceed, as I couldn't return the number closest to the user's input.
Note: The question was asked before loops, functions and arrays were covered, thus I am assuming that these should not be required.
I managed to extract and simplify the problem to the following. Lets say I have the following numbers, and I want to return the number closest to the user's input:
10 20 30 40 50 60 70 80
printf("Enter a number to find closest value: ");
scanf("%d", &num);
For example:
User's input: 28
Closest number: 30
Next, I decided to find the difference between num and each of the numbers by subtracting from each.
difference1 = num - 10;
difference2 = num - 20;
and so on. (I am not using loops as these have not yet been covered in the book)
I taking into consideration, negative differences (25 - 40 = -15). If the difference is less than 0, I am multiplying the difference by -1 to get all the differences to a positive integer. By doing so I will be able to compare the differences successfully:
if (difference1 < 0) {
difference1 = difference1 * -1;
}
Next I am checking for the minimum difference in order to identify which was the closest number by doing:
if (difference1 < difference2) {
min1 = difference1;
}
else {
min1 = difference2;
}
if (difference3 < difference4) {
min2 = difference3;
}
else {
min2 = difference4;
}
if (difference5 < difference6) {
min3 = difference5;
}
else {
min3 = difference6;
}
if (difference7 < difference8) {
min4 = difference7;
}
else {
min4 = difference8;
}
if (min1 < min2) {
min5 = min1;
}
else {
min5 = min2;
}
if (min3 < min4) {
min6 = min3;
}
else {
min6 = min4;
}
if (min5 < min6) {
min = min5;
}
else {
min = min6;
}
I know this is a very long method, however I was unable to shorten the code without the use of a for-loop.
printf("Difference between the two numbers is %d\n", min);
Since min contains the difference between the user's input and the closest number I am unable to find a way to print the number closest to the user's input.
How can I trace the minimum difference to the original number which this was subtracted from ? Any suggestions would be appreciated, and please excuse the basic nature of this question.
I can help you to make the code shorter.
Here are my approach
int num = 71;
int diff,ans;
int div10 = (num / 10);
int lower = div10 * 10;
int upper = (div10 + 1) * 10;
if(lower<10) lower=10;
if(lower>80) lower=80;
if(upper<10) upper=10;
if(upper>80) upper=80;
int diff1 = lower - num;
int diff2 = upper - num;
if(diff1 < 0) diff1 *= -1;
if(diff2 < 0) diff2 *= -1;
if(diff1 < diff2) {
ans = lower;
diff = diff1;
}
else {
ans = upper;
diff = diff2;
}
printf("ans=%d, diff=%d\n",ans,diff);
Well what I would do:
int main(){
int closest;
int temp;
int a0 = 10, a1 = 20, a2 = 30; //and more..
int user_input = 28;
closest = abs(user_input - a0);
temp = abs(user_input - a1);
if (temp <= closest)
closest = temp;
//and so on
return 0;
}
Where abs is absolute value. Numbers should be in some data structure, because in the mean time you should ask if you still have something to check.
If you're dealing with 10s this should be quite simple:
num += 5;
closest_10 = num/10;
closest_10 *= 10;
Then you can have simple look up if you have an array of numbers. You can expand this to other multiples using a similar algorithm (say for 8s just replace 5 with 4 and 10 with).
As all numbers are fixed, the best approach (at least the most efficient once compiled) is to use the following snippet of code:
int closest(int number)
{
switch(number) {
case 0: case 1: case 2: case 3: case 4:
return 0;
case 5: case 6: case 7: case 8: case 9:
case 10: case 11: case 12: case 13: case 14:
return 10;
...
case 95: case 96: case 97: case 98: case 99: case 100:
return 100;
} /* switch */
} /* closest */
Even more efficient if you can inline the code instead of writing a function.
You need to better define: Lets say I have the following numbers.
How many numbers is the big question. Your example has them all as evenly divisible by 10. Your numbers are 10, 20, 30, and so on, and you mention a total of 8 numbers. Is it only 8 numbers maximum? Can they be any number? Will they all be integers? If its all integers and all divisible by 10, then you can use the modulo or modulus operator instead of subtraction.
The mod operator % only works on integers.
int a, b, c;
a = 3;
b = 25
c = a % 10; /* c will equal 3, 0 / 10 = 0 with remainder 3 */
c = b % 10; /* c will equal 5, 25 / 10 = 2 but remainder is 5 */
/* check if a number is even, just mod by 2 if zero then even */
if ( ( b % 2 ) == 0 )
printf(" value in b is even number\n");
else
printf(" value in b is odd number\n");
I just wanted to know why my code doesn't give the right answer.
I've tried it with Euler 7 and it works fine.
Here is my code:
int main(void)
{
int prime = 1;
int number = 3;
int counter = 0;
int sum = 0;
while (number <= 2000000)
{
counter = 0;
for (int i = 2; i < (sqrt(number) + 1); i++)
{
if (number % i == 0)
{
counter++;
}
}
if (counter == 0)
{
printf("Number: %d Prime: %d\n", number, prime);
prime++;
sum = sum + number;
}
number++;
}
printf("The sum is: %d\n", sum);
system("PAUSE");
return (0);
}
Issue 1
The sum of all primes below 2000000 is 142913828922 (~1.4*10^11 ).
The maximum number that fits into an 32bit int is 2147483647 = 2^31-1 (~2.1*10^9).
So you are getting an integer overflow.
Fix 1
unsigned long long sum = 0;
and
printf("The sum is: %lld\n", sum);
should give the expected result.
Issue 2
2 is also a prime, but you are not counting it.
Fix 2
A quick hack would be to initialize sum with 2.
2 is also a prime number. So, you can initialize sum = 2 (according #sergej).
In
if (number % i == 0)
{
counter++;
break;
}
You should use a break for reducing extra iteration because if (number % i == 0) is true then counter value is increase and it will be 1 and also perfectly work.
And you should use unsigned long long int sum = 0 because for 2 million, sum is much higher from 2^31-1 (if you use int).
And you display your sum value by printf("%lld", sum)
Hello Guys I am trying to solve one problem given on the Hacker Rank. Though the problem is quite simple, I was thinking to solve the problem using some other concepts.
The problem is
Desription
You are given an integer N. Find the digits in this number that exactly divide N (division that leaves 0 as remainder) and display their count. For N=24, there are 2 digits (2 & 4). Both of these digits exactly divide 24. So our answer is 2.
Input Format
The first line contains T (the number of test cases), followed by T lines (each containing an integer N).
Constraints
1≤T≤15
0
I solved the problem earlier by defining variable N as of type long long but that i guess will not be the efficient way to solve the problem.
So i thought why not declare the variable N as an character array. This way we can also use the program to store the number greater then the max limit of long long also rt?
Say i used the following code
#include <stdio.h>
#include <string.h>
int main()
{
int i,t;
char n[20];
scanf("%d",&t);
while(t--)
{
scanf("%s",n);
int len=strlen(n);
int f2,f3,f5,f7,f4,count;
f2=f3=f5=f7=f4=count=0;
for( i=0;i<len;++i)
{ int sum=0;
switch((int)n[i])
{
case 48: break;
case 49: ++count;break;
case 50: if((int)n[len-1]%2==0) // divisibility by 2
{
++count;f2=1;
}break;
case 51: for(i=0;n[i]!='\0';++i) // divisibility by 3
{
sum+=(int)n[i];
}
if(sum%3==0)
{
++count;
f3=1;
}break;
case 52: if(f2==1) // divisibility by 4
{
++count;
f4=1;
} break;
case 53: if(n[len-1]=='5' || n[len-1]=='0') // divisibility by 5
{
++count;
f5=1;
}break;
case 54: if(f2==1 && f3==1) // divisibility by 6
{
++count;
break;
}
case 55: // Code for divisibilty by 7
case 56: if(f2==1 && f4==1) // divisibility by 8
{ ++count;
break;
}
case 57: if(f3==1) // divisibility by 9
{
++count;
break;
}
}
}
printf("%d\n",count);
}
return 0;
}
The program is working fine but the only problem is I am not able to rt the code for divisibility by 7 anu suggestions will be helpful, And also which is the better way to solve the problem , This way in which the variable N is declared as the character array or by declaring the variable N as long long.
Any improvements for the above code would also be appreciated .....:)
Divisibility by 7 can be checked by this rule
Also you can use this mod() function to check divisibility by any number :
int mod(char *n, int val)
{
int sum = 0;
for(int i=0; n[i]; i++)
{
sum = sum*10 + (n[i]-'0');
if(sum >= val)
sum = sum % val;
}
return sum;
}
it will return 0, if the number n is divisible by number val :)
And you don't need to check for every redundant digit.
First check the available digit then check for divisibility once for each digit.
Here's what you can do -
#include <stdio.h>
#include <string.h>
int mod(char *n, int val)
{
int sum = 0;
for(int i=0; n[i]; i++)
{
sum = sum*10 + (n[i]-'0');
if(sum >= val)
sum = sum % val;
}
return sum;
}
int main()
{
int i,t;
int digit[10];
char n[20];
scanf("%d",&t);
while(t--)
{
scanf("%s",n);
int len=strlen(n);
int cnt=0;
memset(digit,0,sizeof(digit)); // setting all the digit to 0
for(i=0;i<len;i++)
digit[n[i]-'0']++;
for(i=1;i<10;i++)
{
if(digit[i]==0) // number doesn't contain any of this digit
continue;
if(mod(n,i)==0)
cnt+=digit[i]; // Adding the digit to the answer
}
printf("%d\n",cnt);
}
return 0;
}
How this works :
for n = 147 and val = 7
sum = 0
1st iter >> sum = 0*10 + 1 = 1
sum < val, so continue
2nd iter >> sum = 1*10 + 4 = 14
sum >= val, so sum = sum % val = 14 % 7 = 0
3rd iter >> sum = 0*10 + 7 = 7
sum >= val, so sum = sum % val = 7 % 7 = 0
as the final sum is 0, so we can say that n is divisible by val :)