Integer Numbers in C
Ok, this is kind of a weird question. But how can I tell the compiler something like this
a = 3/2 if a is not integer then it is a prime number and print it. Better said, how can the compiler tell if "a" is integer or not? Let me guess...it can't... |
Quote:
|
Yes, but the problem is with the flow of the program. I want something similar like this
if(a is integer) { printf("%d is a prime number\n", a); } Is there a way to check if a is integer? I know it is integer, but how can I do it in programming? |
C being a language where all variables must be declared and typed, your test doesn't make sense, a is either already, or never will be an integer (i.e. declared as an int).
Perhaps are you asking for some algorithm that will tell is some double precision number is also equal to an integer, in that case look at the ceil and floor functions, but beware that due to rounding errors, you cannot be totally sure. Also you cannot print a double with %d, you must cast your number to an int, or use %ld as a format. |
The modulus operator (%) returns the remainder left after division. So if you have num1 % num2 and the result is 0, then num2 divides evenly into num1.
Does that help? |
Yes, that helped, i'll see now for the implementation.
|
Well, basicly I want this. I want to take a number, divide it by 2, and if the result is not an integer, then for sure that number is prime and then I want to print it. Problem is I need to check if the result is an integer or not.
|
(integer)/(integer) is always an integer in C.:
Code:
#include <stdio.h> Code:
skalkoto@darkstar:~/src$ ./a.out |
Basically:
3/2=1 but 3.0/2.0 or 3/2.0 or 3.0/2 equals 1.5 |
Got that, I have to use floats, or I can implement that with the "%".
|
0 % 2 = 0
1 % 2 = 1 2 % 2 = 0 3 % 2 = 1 4 % 2 = 0 Etc. |
Quote:
A number not divedable by 2 is an odd number. Not a prime. 15 is not a prime and not dividable by 2. You cannot decide if a number is prime without knowing the prime numbers smaller than this one. All you can do is start computing the prime numbers and check if this one is one of the list or something like that, but you cannot decide without knowing the smaller prime numbers. It's already proven that this is an unsolvable problem back in the 40's or 50's. |
Quote:
There are many ways to test a number for "primeness," but the number of operation required to do so increases exponentially with the size of the number being tested. (It's an "NP-complete" problem.) Try "google" for details. Oh, a quick test for the parity of an integer is to look at its last bit. If it's zero, the number is even, if one, it's odd. That is, for an integer, just use Code:
inline logical test(int a) {return (a & 1);} |
You are right, I was wrong :) 15 can be divided by 3. How do you suggest to compute if one number is prime or not?
Got it, thanks alot for the help. One last question about flowcontrol. Cant I do in C something like go to line 15? |
Quote:
Code:
goto you put a <tag> in line 15, and you use "goto <tag>" Code:
static int cpio_mkgeneric_line(const char *line, enum generic_types gt) |
All times are GMT -5. The time now is 12:17 PM. |