الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

برنامج يختبر هل الرقم اولي ام لا ثم يطبع عوامله اذا كان غير اولي

مغلق
بدأه Kmarwani في 8 أكتوبر 2007 · 8 رد · 8,824 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

كما ذكرت سابقا انا مبتدئ في الجافا (يعني مازلت اتعلم اساسيات الأساسيات :))

عندي (Assignment) واجب ساحاول حله في هذا الموضوع .. لسببين لكي يستفيد المبتدئون امثالي ..والسبب الثاني لإستفيد من نصائحكم وتصحيحاتكم

المطلوب

برنامج يقرأ من المستخدم رقم (Integer) موجب ثم بعد ذلك يقوم باختبار هل هذا الرقم اولى ام لا ؟ اذا كان اولي يطبع (الرقم س اولي ) اذا كان غير ذلك (مركب) يقوم بطباعة العوامل (الارقام التي تقبل القسمة بدون باقي ) لهذا الرقم .. ويستمر بالطلب في كل مره ينتهي من الطباعة ، يتوقف البرنامج في حال ادخل المستخدم -1

------------------------------------------------------------

اولا ساقوم بانشاء Method تستقبل رقم وتختبره هل هو اولى ام لا ؟

public static boolean mIsPrime ( int num )
 {
 int limit = (int) Math.sqrt ( num );  

  for ( int i = 2; i <= limit; i++ )
   if ( num % i == 0 )
	return false;
 return true;
  }
#2

حسب كلام احد الدكاترة يدرسني Math

يقول ما فيه خوارزمية algorithm لحساب الأعداد الأولية

كثير علماء طلعوا خوارزميات لكن كلها يصير فيها أخطاء ..

أنا ما اقتنعت بكلامه ,,لكن في نفس الوقت ما جربت او حاولت

#3

بداية موفقة يا أخي الكريم

هناك عملية توفر عليك نصف اللفات المطلوبةلاحظ معي أن الأعداد الزوجية تمثل نصف الأعداد وهذه ستضمن أنك لن تستفيد منها بمجرد القسمة على 2

أظن يوجد خوارزميات أفضل من ذلك أيضاً ولكنها ترتبط بمفاهيم رياضية مقعدة لم أستطع استيعابها

بالنسبة للأخ الذي يقول بأن الخوارزميات للأعداد الأولية تعطي نتائج خاطئة أقول له التالي

الخوارزميات لفحص الأعداد الأولية تنقسم إلى قسمين

نتائج أكيدة والخوارزمية التي ذكرها الأخ أحدها

نتائج محتملة تعطي أعداد محتملت الأولية وهذه الخوارزميات تعتمد على علم الاحتمالات فهي لا تضمن أن العدد أولي ولكنها تضمن أن احتمالية الأولوية له عالية تصل في بعض الخورازميات إلى 75% على حد علمي طبعاُ

بالنسبة لماذا وجد النوع الثاني إن كانت النتائج له غير دقيقة؟؟

الحاجة إلى توليد أعداد أولية كبيرة جداً مما يجعل خوارزمية بتعقيد جذر الـ n خوارزمية سيئة جداً

للتعمق في الحاجة إلى الأعداد الأولية الكبيرة ممكن مراجعة مجال الأمان خوارزميات المفتاح العام public key

الله معكم

O((log n)^12).AKS primality test

هو أسرع خوارزمية معروفة حالياً على النوع الأول

تم تعديل هذه المشاركة بواسطة alaadiaa في 17 أكتوبر 2007 في 06:39

حزمة المحرك الإصدارة 0.8

أي أحد يجد أني ظلمته فليراسلني

وبإذن الله لو كان له حق سيأخذه

728x90.png

#4

هتستخدم ال recursion

#5

اعتذر عن التأخير

لم اكن اعلم ان المنتدى عاد للعمل من جديد

عوداً حميدا

المهم بالنسبه للواجب .. خلصته وسلمته ..

كنت اتمنى اسوية خطوة خطوة ..

واستفيد من النصائح ..

لكن ماحصل وان شاء الله في واجب ثاني

-----------------------------------------------

اجابة على تسائل .. لماذا وضعت بعض

loops

او لماذا احاول ان اطبع بطريقة معينة

السبب ان الواجب يتضمن

output

مفترض للبرنامج لذلك حاولت ان يكون مطابق له

خصوصا عند طباعة العوامل للرقم المركب

حيث كانت تحتوي على عمودين احدهما يبدأ من اصغر عامل ويزيد والآخر من الرقم نفسه وينقص

/*************************************************
Name: PrimeNumbers.java
Author: kmarwani
Date: 
Built on: JCreator4.5/jdk 1.6
Description: This program  will prompt the user for a number and determine whether or not it is prime. if isn’t it  will output the factors of a composite number. 

************************************************/

import java.util.Scanner;

public class PrimeNumbers
 {
  // creat a new type
  private enum Status { CONTINUE, EXIT };

  public static void main(String[] args)
	  {
		  int iUserNum, // the number which will be enter by the user
		iLimitCounter, // the limit of the increasing counter in case printing factors 
		iDecreaseCounter; // decreasing counter for printing factors 
	   Status LoopStatus; // To decide whether we stay in decreasing counter or leave it

		 // Print Student Info.


	 //print the purpose of the program 
	 System.out.printf("This program will determine a number whether or not it is prime. if it isn’t the output will be the factors of a composite number.\n\n");

		 // creat scanner  to read a number
			  Scanner input = new Scanner(System.in);		


				  // msg tells which numbers are acceptable and -1 for exit
				   System.out.print("Please enter a positive integer greater than 2 or a -1 to quite.\n");
				   iUserNum = input.nextInt(); //prompt


				   while ((iUserNum !=-1 )&& (iUserNum > 2))// we will continue asking the user for a number until he enter -1 
					{

					  if (mIsPrime(iUserNum)) // call method mIsprime to cheack if the num is prime or not ?
					   {
						  // if the num is prime print msg to tell that and line before and after 
		System.out.println(); 
				 System.out.println(iUserNum + " is a prime number");
				 System.out.println();
			   }   
			   else // if not that mean the num is composite   
				{
				 // a new line and msg telling the num is not a prime and its factors are :
				 System.out.println();
					 System.out.println(iUserNum + " is not a prime number. Its factors are:");

							  // Assign  the value of number to LimitCounter and iDecreaseCounter	
					 iLimitCounter = iUserNum;
					 iDecreaseCounter = iUserNum;

					 // start loop to print the factors
					 for ( int i = 1; i <= iLimitCounter; i++ ) // i start from minimum 
					  {


					  if ( iUserNum % i == 0 )// if the mod=0  after division that mean i is a factor
					   {
			   System.out.printf("\n%d ",i); // print factor 
						LoopStatus = Status.CONTINUE; // assign loopstatus to CONTINUE

						// another loop to print factors -- this loop from max 
						// check loop status and and counter is greater than the other loop or not 
						while ((iDecreaseCounter > i ) && (LoopStatus == Status.CONTINUE))
						 {
						  if (iUserNum % iDecreaseCounter == 0) // if the mod=0  after division that mean iDecreaseCounter is a factor
						   {
							System.out.printf("%d",iDecreaseCounter);// print factor
							LoopStatus = Status.EXIT; // change loop status so next print will not be on the same clm.
						   } 

						  iDecreaseCounter--; // decrease the counter

						 } //end while decreasing loop 

						iLimitCounter = iDecreaseCounter; //assign new value of the counter to the limit 

					   } //end if which print factors increasing loop

					  } // end for loop
		 System.out.println();
				} // end else which print factors				   

			   // prompt user to enter a new num
			   System.out.print("Please enter a positive integer greater than 2 or a -1 to quit.\n");
					 iUserNum = input.nextInt();
				} //end while which check if the user enter -1 or not		   

			 } // end main 

	  public static boolean mIsPrime ( int iNum ) // method check if a num is prime or not ?
	{
	 int iLimit = (int) Math.sqrt ( iNum );  // Declare  the limit which can be sgr root of the num 
	 for ( int i = 2; i <= iLimit; i++ ) // sart the loop
	  if ( iNum % i == 0 ) // if the mod=0  for any  division that mean num  is not prime
	   return false; // so return false 
	 return true; // if complet loop without find mod = 0 that mean num is prime
	  } // end mIsPrime method

 }   // end class
#6

السلام عليكم ورحمه الله وبركاته

انا ايضا مبتداه في تعلم لغه الجافا واتمني منك الاستمرار

اما عن الكود انا اعددته بهذا الشكل

import java.util.Scanner;

public class PrimeNum {

	public static void main(String[] args) {

	 // TODO, add your application code
	 Scanner s=new Scanner(System.in);
	 System.out.print("Enter Number To test it : ");
	 int num=s.nextInt();
	 int y=0;
	 for(int i=2; i<=num/2; i++){
	  if(num%i==0){
	   System.out.println("num "+num+" not prime divisible by "+i);
	   y++;
	  }
	  if(y==0){
	   System.out.println("num "+num+" is prime ");
	  }
	 }
	}
}

تم تعديل هذه المشاركة بواسطة kinzy في 19 أكتوبر 2007 في 22:12

#7

السلام عليكم ورحمة الله

أخت kinzy الكود صحيح تماماً

لكن هناك خوارزميات أفضل

منها ما كتبه الأخ كاتب الموضوع

هناك ما هو أفضل من ذلك أيضاً

بالنسبة للأخ مبتدئ جافا

هل تعرف أخي الكريم شيئاً عن تلك الخوارزميات التي لا تعتمد على الاحتمالات

أتمنى لو كان لا يشق عليك ان تذكر لنا القليل عنها

حزمة المحرك الإصدارة 0.8

أي أحد يجد أني ظلمته فليراسلني

وبإذن الله لو كان له حق سيأخذه

728x90.png

#8

خوارزميات وهالكلام ما أفهمه ,, لو تقولها بالإنقليزي ممكن أفهم شوي لاني درست البرمجة كلها بالإنقليزي

هذا حلي للمسألة بكل بساطة

import java.util.*;
public class isPrime
{
	public static void main(String[] args)
	{
		Scanner k = new Scanner(System.in);
		System.out.println("Please Enter a number");
		int number = k.nextInt();
		int check=0;
		boolean isPrime=mIsPrime(number);

		if(isPrime==true)
			System.out.println("number is prime number");
		else
			{
				System.out.println("Number Dividors Are:");

				for(int i=1;i<=number;i++)
					{
						check =number%i;
						if(check==0)
						System.out.println(i);
					}
			}	
	}

	public static boolean mIsPrime ( int num )
		{
			int limit = (int) Math.sqrt ( num ); 

			for ( int i = 2; i <= limit; i++ )
			if ( num % i == 0 )
			return false;
			return true;
		}

}
#9

لم أظن أن كلمة خوارزميات تجهل على أحد لذلك لم أكتب ترجمة

الكلمة تعني algorithm والكلمة مشتقة من بلدة خوارزم التي كانت مسقط رأس العالم العربي الجليل الخوارزمي

الله معكم

حزمة المحرك الإصدارة 0.8

أي أحد يجد أني ظلمته فليراسلني

وبإذن الله لو كان له حق سيأخذه

728x90.png

هذا الموضوع مغلق.

مواضيع مشابهة

عدد الزوار حالياً

المتواجدون خلال آخر دقيقتين · يتحدّث كل ٣٠ ثانية

—الإجمالي—أعضاء مسجّلون—زوار بدون تسجيل

جارٍ التحقق من المتواجدين…