Environment
I want to introduce an algorithm called “Sieve of Eratosthenes”. This algorithm can screen prime numbers. You can get prime numbers as many as you want in the range of RAM of your PC. One way you can do programming like below download an IDE, or Integrated Drive environment.copy the source code below on your editor of the IDE and start the program. I recommend you some IDEs below:“Easy IDEC”
http://9cguide.appspot.com/index.html
This is for beginners. I started studying C language with this IDE. The set language is Japanese only. The IDE is very simple so it is very easy to use. If you can read Japanese, I recommend this IDE. If you get accustomed in C language, the “Microsoft Visual Studio Express” below will be a good choice to change your IDE.
“Microsoft Visual Studio Express 2013 for Windows Desktop”
http://www.visualstudio.com/ja-jp/products/visual-studio-express-vs.aspx
This is a free addition of “Microsoft Visual Studio” from Micro Soft. Only “Express” is free, so you must be careful when you choose downloading file. Even though it is free addition, it has many functions which is enough to do programming as a hobby.
If you downloaded your favorite IDE, or you already have a IDE on your PC, make a project titled “Sieve_of_Eratosthenes”. The program can screen prime numbers from 1 to 100000. copy the source code below on your editor of the IDE
The
code I wrote below:
|
/* * 2014/12/03 */ #include <stdio.h> #define MAX 100000 int main() { int i, j; int array[MAX + 1]; for(i = 0; i < MAX; i++){ array[i] = 1; } for(i = 2; i * i <= MAX; i++){ for(j = 2; j <= MAX / i; j++){ array[i * j]=0; } } for(i = 2; i < MAX; i++){ if(array[i]){ printf("%d\n",i); } } return 0; } |
Grammer
When I wrote this code, I used an algorithm called “Sieve_of_Eratosthenes”.
The mechanism is setting “flags” and, if a number is a multiple of two numbers,
clear the “flag”. The “flag” is a bit information, represents “true” or “false”.
In the code above, I used “array” as the “flag”. In the C language , “array” is
consecutive of “variables” of one “data
type”. The “data type” is forms of values. In computers, every numbers has it’s
own “address”, and the format of each numbers being written in an address are
strictly certain. In the code, “int” is a kind of type meaning integer. Characters “i, j” following “int” type is “variables”
used to put numbers during calculations.
source | meaning |
array[i] = 1; | Set 1 to variable array[i] (array: “pointer”, spotting address array[i]: i th int type value of int type “array”) |
int main();//definition
printf("%d\n",i);//already defined in the header file stdio.h |
Functions of the C language. “return value type” “name of the function” “arguments” |
for(i = 2; i * i <= MAX; i++){ for(j = 2; j <= MAX / i; j++){ array[i * j]=0; } } |
#define MAX 1000 <=>MAX == 1000. for(j = 2; j <= MAX / i; j++){ array[i * j] = 0; }<=>repeat “array[i * j] = 0;” while i is from 2 to MAX/i by 1; |
printf("%d\n",i); | Output int type value “i”. |
The C language has a lot of functions which enables you to know the computers more deeply and enables you to have a great ability to calculate as you want!
References:
http://9cguide.appspot.com/
http://www9.plala.or.jp/sgwr-t/lib/libtop.html
http://ja.wikipedia.org/wiki/%E3%82%A8%E3%83%A9%E3%83%88%E3%82%B9%E3%83%86%E3%83%8D%E3%82%B9%E3%81%AE%E7%AF%A9
0 件のコメント:
コメントを投稿
コメント表示は承認制に設定しています