Jump to content
Sign in to follow this  
paradox

Please Help Me

Recommended Posts

Kepada Yang Sememangnya pandai c tu toyong ajar aku leh tak......

Nie soklan dia...aku dah blur memikirkannya macam maner dia berfungsi...

in this program,u will di programs frm 2 algorithms given to u.

1.the sieve of eratosthenes

the most efficient way to find all of the small primes (Say alll those less than 10,000,000) is by using the Sieve of Eratosthenes

Make a list of all the integers less than or equal to n(and greater than one).strike out the multiples of all primes less than or equal to the square root of n,then the nymbers thaht are left are the primes.

please help me...

a.s.a.p

Edited by paradox

Share this post


Link to post
Share on other sites

Tu cool dah bagi link dah baca lum??

What is the Sieve of Eratosthenes?

A prime number is a natural number greater than 1 that can be divided without remainder only by itself and by 1. Natural numbers n that can be divided by a number less than n and greater than 1 are composite numbers. The Sieve of Eratosthenes identifies all prime numbers up to a given number n as follows:

Write down the numbers 1, 2, 3, ..., n. We will eliminate composites by marking them. Initially all numbers are unmarked.

Mark the number 1 as special (it is neither prime nor composite).

Set k=1. Until k exceeds or equals the square root of n do this:

Find the first number in the list greater than k that has not been identified as composite. (The very first number so found is 2.) Call it m. Mark the numbers

2m, 3m, 4m, ...

as composite. (Thus in the first run we mark all even numbers greater than 2. In the second run we mark all multiples of 3 greater than 3.)

m is a prime number. Put it on your list.

Set k=m and repeat.

Put the remaining unmarked numbers in the sequence on your list of prime numbers.

Ko faham tak quote kat atas nie?? Dan ko dah try ker buat?? Boleh bagi ape yang ko dah buat then baru aku tolong2 betulkan kalau tak betul... Dan tolong bagitau sekali ape yang ko paham dari quote kat atas.. Tak betul aku tolong betulkan..

smile.gif

Edited by b1naryc0de

Share this post


Link to post
Share on other sites

#include <stdio.h>

#include <math.h>

main()

{

int a,b,num;

float num1,num2;

double num3;

printf("Please Enter Your Number");

printf("\nFirst Number : ");

scanf("%d",&num1);

printf("Second Number :");

scanf("%d",&num2);

num3=sqrt(num2);

for(a<1;a<num1;++a)

{

if (num%2==0)

{

b=0;

}

else

{

b=1;

}

if (b==1)

num3 = sqrt(num);

}

//kita nak cari even number....

printf("

}

Nie aper yang aku dah buat...so sekarang nie ada apa² masalah tak kat sinie

Share this post


Link to post
Share on other sites

Aku tak pasti ape yang ko cuba buat kat coding tue.. unsure.gif

Contoh:

for(a<1;a<num1;++a)

dan

num3 = sqrt(num);

num tue berapa??

Boleh explain??

//kita nak cari even number....

kenapa nak cari even number?? 15 odd number kan?? tapi nape 15 bukan prime number?? Jadi ko tak perlu cari odd atau even number..

Ko sekarang tengah buat algoritma kan?? Jadi ko kene lakar algoritma dia camner dulu baru fikir coding dia camner..

Oklah.. Aku bagi tips sikitlah:

contoh cari nombor perdana dari 1 - 10...

senaraikan nombor 1-10 dalam array unmark[10];
isytihar komposit[10],perdana[10];
komposit[0]=1;
unmark[0]=0; ganti nilai unmark[0] dari 1 ke 0;
prime = 2;
perdana[0]=2;
unmark[1]=0; ganti nilai unmark[1] dari 2 ke 0;
k=1 <- set subscript untuk komposit[] sebab [0] dah isi;
p=1 <- set subscript untuk perdana[] " " " ";
do
{
	//keluarkan nombor komposit
	for (i=2;i<(10/2);i++)
	{
  if ((prime*i) > 10) then
  	break; <keluar loop kalo hasil lebih dari 10
  else
  {
  	komposit[k]=prime * i;

  	//buang nombor komposit dari unmark[];
  	for (j=0; j<10;j++)
  	{
    if (komposit[k] == unmark[j]) then
      unmark[j]=0;
  	}
  }
  k++;
	}

	//periksa nombor prime yang seterusnya
	for (i=0;i<10;i++)
	{
  //cari no terkecil yang masih ada dalam unmark selain 0
  if (unmark[i] != 0) then
  {
  	prime = unmark[i];
  	perdana[p] = prime; <- simpan no perdana;
  	p++;
  	break;<-keluar for() lepas jumpa prime berikutnya
  }
	}
}
while (prime < sqrt(10)) <- (prime < 5)

//nombor yang masih dalam unmark[] adalah nombor perdana.. Jadi masukkan nombor2 tue dalam perdana[] lepas dah habis loop kat atas..

Aku buat nie dalam keadaan separuh ngantuk.. Kalo2 ko perasan ada silap bgtau aku atau ko betul2kanlah sendiri..

WAaaaAArgh ngantuk.. Aku gi tdo dulu..

Share this post


Link to post
Share on other sites

condotion 1

Kena generate 2 number....

for the 2 random number number 1 must be smallerthan the second number...

the number generated must be different each time

contohnyer ler...

generating 2 random number number....

first number = 97

second number = 120

pastu

find all prive number between the first number and the second number

condition

the number generates must be prime numbers

sample program between 97 and 250 are

97 101 103 107 113

Share this post


Link to post
Share on other sites

Ko boleh guna contoh kat atas.. Cuma ko ubah sikit2 lah..

Katalah 1st number ko set sebagai A

jadi mula2 ko check A tue prime ker tak guna contoh kat atas tapi ganti 10 tu dengan A.. Kalo prime ko startlah dari 97 tu ganti prime = 2 kat atas dan ganti 10 tue dengan B(nombor kedua)..

Kalo 97 bukan prime check lak nombor berikutnya.. Dan kalo nombor tue prime ko start buat macam kat atas dengan set prime = nom tue..

Tapi memandangkan jumlah dia tak tetap ko gantilah senarai[] tue guna ape2 loop sebab kalo pakai senarai[] jumlah yang dia simpan kene tetap.. Ganti gak semua array dengan looping..

Ko cuba buat dulu.. Ari nie lepas kul 12 mlm baru aku boleh reply..

smile.gif

Edited by b1naryc0de

Share this post


Link to post
Share on other sites

senang aku citer ler.....contoh nyer kita letak

first number : 2

second number : 20

dia akan cari number dari 2 hingga

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

memula di cari gandan 2,kemudian 3,kemudian 4 ...

gandaan 5 tak boleh sebab punca kuasa 2 bagi 20 =4.47... ianya mesti less than this number kan.....

pastu kita kan dapat num yang tak gi ignore...tu...betoi tak....

Edited by paradox

Share this post


Link to post
Share on other sites

Yep betoi.. tambahan.. yang no first tue kene tengok dulu prime ke composite.. Sebab macam nombor 2 memang kita tau prime jadi takde masalah kita nak start dari dua.. Ini kalo ikut contoh kat atas la..

memula di cari gandan 2,kemudian 3,kemudian 4 ...

Situ ko ada silap sikit.. Kita bukan cari gandaan untuk semua 2,3,4,5,6,... Cuma cari gandaan nombor yang belum ditanda sebagai composite..

contoh untuk 1-10

1 dah mark sebagai composite

so start dari 2 dapat mark 4,6,8,10

belum mark 3 jd cari gandaan 3 dapat 6,9

belum mark 5 jd cari gandaan 5 dpt 10

yang tinggal 7

jadi kita dapat 2,3,5,7 sebagai prime..

sbnrnya ada formula lain.. tgk gandaan 3 dan 5 ada no yang dah dimark sebelum tue.. Jadi contoh aku bagi tue ada silap sikit.. Ko kene ubah sikit macam buang nombor yang sama ker ape ker..

Ikut contoh kat atas ada nombor berganda dalam komposit[].. Tapi kalo setakat nak display prime number masalah nombor sama dalam komposit[] boleh diabaikan.. Sebab yang penting untuk cari prime number adalah unmark[] dan perdana[]..

smile.gif

Edited by b1naryc0de

Share this post


Link to post
Share on other sites

senaraikan nombor 1-10 dalam array unmark[10];

isytihar komposit[10],perdana[10];

komposit[0]=1;

unmark[0]=0; ganti nilai unmark[0] dari 1 ke 0;

prime = 2;

perdana[0]=2;

unmark[1]=0; ganti nilai unmark[1] dari 2 ke 0;

k=1 <- set subscript untuk komposit[] sebab [0] dah isi;

p=1 <- set subscript untuk perdana[] " " " ";

do

aku masih blur lagi kat sinie...... kalau aku letak 1000 hingga 3000 number yang besor.....56321 hingga ler 86453 tu macam maner plak...

(say all those less than 10,000,000)

Edited by paradox

Share this post


Link to post
Share on other sites

Ko dah belajar guna file?? Kalo ko tau gune file ko boleh tukar array tu ke file..

Ataupun nak senang camnilah.. ko still check dari prime = 2.. Tapi yang unmark tue ko ubah dalam bentuk loop guna for ker..

Lepas tue ko tambah if supaya dia paparkan prime number >= num1 sahaja..

Ok??

Dengan kata lain ko cari prime number

2,3,4,5,6,7,..........num2

mark gandaan prime = (2, diikuti number yang belum di mark) hingga sqrt(num2)

display nombor yang tak dimark >= num1

Sorrylah aku tak dapat bagi contoh skrg.. Good Luck..

Edited by b1naryc0de

Share this post


Link to post
Share on other sites

secara jujur nya aku memang suka tapi sebab lecture yang baru nie diskrimirasi antara pelajar lain...mejority study bukan berbangsa melayu... bila aku tanya define hari tu dia cakap who do it? my friends teach me...okay first u understand about this and then study ur self.... boleh dia kata macam tu...... aku nak tanya tentang #define jer......

bila pelajar sekaum dengan dia tanya siap dia buat planning .....suruh mula macam nie....pastu letak task nie.... aku tengok sendiri dengan mata aku...tension betoi aku tengok......lagi pun dia cepat kan submit assessment kalau tidak bulan depan..tapi dia nak minggu depan sebab bulan depan dia nak pergi US... ada ker patut...

topic yang aku dah blajar adalah array,pointer and character string...

aku tak nak ler guna return ker,aper yg high lvl dari tu....sebab kalau interview dia tanya macam nie...kang mati.. plak....

Edited by paradox

Share this post


Link to post
Share on other sites

#include <stdio.h>
#include <math.h>

main()
{
	int a,b,num;
	float num1,num2;
	double num3;

	printf("Please Enter Your Number");

	printf("\nFirst Number : ");
	scanf("%d",&num1);
	printf("Second Number :");
	scanf("%d",&num2);

	num3=sqrt(num2);
lepas pada nie kena buat looping bagi nak cari number
for (i=2;i<(10/2);i++)< sini kena tukar kepada number [b]num1[/b]
{
 if ((prime*i) > 10) then< kena tukar kepada [b]num2[/b] sebab [b]num1[/b] kecik daripada [b]num2[/b]
  break; 
 else
 {
  komposit[k]=prime * i;

  //buang nombor komposit dari unmark[];<--sini masih blur...
  for (j=0; j<10;j++)
  {
   if (komposit[k] == unmark[j]) then
     unmark[j]=0;
  }
 }
 k++;
}

saper leh kasi pandangan kowang

Edited by paradox

Share this post


Link to post
Share on other sites

secara jujur nya aku memang suka tapi sebab lecture yang baru nie diskrimirasi antara pelajar lain...mejority study bukan berbangsa melayu... bila aku tanya define hari tu dia cakap who do it? my friends teach me...okay first u understand about this and then study ur self.... boleh dia kata macam tu...... aku nak tanya tentang #define jer......

bila pelajar sekaum dengan dia tanya siap dia buat planning .....suruh mula macam nie....pastu letak task nie.... aku tengok sendiri dengan mata aku...tension betoi aku tengok......lagi pun dia cepat kan submit assessment kalau tidak bulan depan..tapi dia nak minggu depan sebab bulan depan dia nak pergi US... ada ker patut...

topic yang aku dah blajar adalah array,pointer and character string...

aku tak nak ler guna return ker,aper yg high lvl dari tu....sebab kalau interview dia tanya macam nie...kang mati.. plak....

Ko study kat ner?? Lect ko bangsa ape?? Kalo dia jadi lect aku dah lama kene sound.. dan tempat ko tak boleh tukar lect ker??

Aku cubalah tolong ko.. Dalam masa yang sama aku tolong asah otak aku gak hehe tongue.gif Tapi sekarang aku nak rehat jap.. Penatlah baru balik.. Nak fikir final project lagi (baru start).. Kalo aku tak sempat tolong ari nie.. Ari sabtu aku tolong.. Tapi ko pon kenelah ada usaha sendiri.. Yang penting usaha buat..

Share this post


Link to post
Share on other sites

Ko study kat ner?? Lect ko bangsa ape?? Kalo dia jadi lect aku dah lama kene sound.. dan tempat ko tak boleh tukar lect ker??

aku study kat PSDC atau ko leh masuk psdc

lecture aku dulu bangsa KITA sekarang nie tukar kepada bangsa Cina.... Lecture yang dulu baik..tapi kalau dia marah jangan kata aper ler.....aku maner leh tukar lecture dah diaowang buat jadual tak leh kata aper ler..... bukan bangsa KITA yang pegang kat situ...

Aku cubalah tolong ko.. Dalam masa yang sama aku tolong asah otak aku gak hehe  tongue.gif  Tapi sekarang aku nak rehat jap.. Penatlah baru balik.. Nak fikir final project lagi (baru start).. Kalo aku tak sempat tolong ari nie.. Ari sabtu aku tolong.. Tapi ko pon kenelah ada usaha sendiri.. Yang penting usaha buat..

Dalam 2 minggu lagi kena hantar 3 assessment yang perlu disiapkan... 1st july nak kena hantar proposal untuk final project...kalau ko leh bagi idea project yang hendak buat...... okay jer......aku amik course mechanical and mechatonic..... pikir nak bikin robot..tapi robot tu banyak jenis...kang buat...cost mahal...mati....

Terima kasih kerna nak asah otak aku dalam bidang c programming nie...aku bersyukur dapat kawan macam nie....

Edited by paradox

Share this post


Link to post
Share on other sites

Aku ada try buat program yang ko kene buat tadi.. Tapi ada bug sikit yang belum dibetulkan.. Ada beberapa nombor yang dia ada masalah atau sangkut.. Harap yang lain dapat betulkan..

*air tak de lak kat umah.. Kene kuar gi tapau..

Ni kod dia:

/*what the Sieve of Eratosthenes says:

Make a list of all the integers less than or equal to n (and greater than one). 
Strike out the multiples of all primes less than or equal to the square root of n. 
Then the numbers that are left are the primes

condition for this program

1) generate 2 numbers where num2 is greater than num1
2) maximum is 10,000,000
3) only generate prime number

What do we need?

An long integer type of array[10,000,000 - 1] to list all the integer greater than
1 and less or equal to 10,000,000

The problem is array[9999999] is to big and need a lot of memory. And we are
not using all the index of that array. So we will use malloc and pointer for
better memory allocation.

We use long int because int only support integer number from -32768 to 32767
(16 bits) while long int support from -4294967296 to 4294967295*/

#include <stdio.h>
#include <math.h> //for sqrt function

void main(){
	long int num1,num2,num3,*list,i,j=2,prime=2,current_prime;
	short greater = 0;

	list = (long int *) malloc(sizeof(long int) *10000000);

//input number range and make sure num2 is greater than num1
	printf("Please enter your number");
	do
	{
  printf("\nYour first number: ");
  scanf("%d", &num1);
  printf("Your second number: ");
  scanf("%d", &num2);
  if (num2>num1)
  {
  	greater = 1;
  }
  else
  {
  	printf("\n*Please enter second number greater than first number*\n");
  }
	}while(greater==0);

//Start to find prime number using Sieve of Eratosthenes
	num3=sqrt(num2); //get the square root of maximum number
	//printf("num3[%d]  ",num3);

//List all the integer number from 2 to num2
	for(i=0;i<(num2 - 1);i++)
	{
  list[i] = j;
  j++;
  //printf("[%d]  ",list[i]);
	}

//Sieve of eratosthenes (strike out all composite number)
	do
	{
  //printf("[%d]  ",prime);
  for(i=2;i<=num2-1;i++)
  {
  	if ((prime*i)>num2)
    break; //exit if prime*i exceed maximum number
  
  	//mark composite number(prime*i)
  	for(j=0;j<num2;j++)
  	{
    if (list[j] == (prime*i))
    	list[j] = 0;
  	}
  }

  //find the next prime
  current_prime = prime;
  for(j=0;j<=num3;j++)
  {
  	if(list[j] != 0)
  	{
    if (list[j] != prime)
    {
    	prime = list[j];
    }
  	}
  	if (prime>current_prime)
  	{
    break;
  	}
  }
  //printf("p[%d]  ",prime);
  //printf("num3[%d]  ",num3);
	}while(prime<=num3);

//Print out all unmark number(prime number)

	printf("\n");
	printf("Prime number for %d to %d:\n\n",num1,num2);

	if (num1<2)
  num1 = 2;

	j=1;

	for(i=num1-2;i<num2-1;i++)
	{
  if(list[i] != 0) //if number is not marked by 0
  {	
  	printf("[%d]  ",list[i]);
  	if (j==10)
  	{
    printf("\n");
    j=0;
  	}
  	j++;
  }
	}

	printf("\n");
}

Hehe ada beberapa printf aku komenkan (//).. Tue nak tengok mane silap..

Edited by b1naryc0de

Share this post


Link to post
Share on other sites

void main(){
	long int num1,num2,num3,*list,i,j=2,prime=2,current_prime;
	short greater = 0;
kat sini ada error tapi aku dah tukar...... double num3;
	list = (long int *) malloc(sizeof(long int) *10000000);

'malloc' : undeclared identifier

bila aku declare pung tak leh jugak....

term does not evaluate to a function

so macam maner tu?

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...