Narisa.com: ปัญหาเขียนโปรแกรมตอนสาม - Narisa.com

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

ปัญหาเขียนโปรแกรมตอนสาม หาค่า prime factors Rate Topic: -----

#1 User is offline   นายข้าวโพดหวาน 

  • Committee
  • View blog
  • Group: Committee
  • Posts: 7075
  • Joined: 01-April 03

Posted 21 March 2009 - 10:59 AM

ต่อจากตอนที่แล้ว คราวนี้เป็นปัญหาเรื่องตัวเลขอีกแล้ว โจทย์คือให้หาค่า prime factors ของเลขตั้งแต่ 2 ถึง 99 โดยค่า prime factor ของตัวเลข n คือค่า prime number ที่หารเลข n ได้ลงตัว และค่าเลข prime number คือเลขที่ไม่มีใครหารมันได้ลงตัวยกเว้น 1 กับตัวมันเอง เช่น 2, 3, 5, 7, 11 เป็นต้น

ยกตัวอย่างเช่น ค่า 99 มาจาก 3 * 3 * 11 จะมีค่า prime factors คือ 3 กับ 11 ถ้าเป็นค่า 100 มาจาก 2 * 2 * 5 * 5 มีค่า prime factors คือ 2 กับ 5

และอย่างเช่นเคย โจทย์ครั้งนี้จะเขียนด้วยภาษาอะไรก็ได้ เน้นการอ่านให้เข้าใจง่าย และได้ผลลัพธ์ถูกต้อง (ดูตารางค่า prime factors ตั้งแต่ 1 - 1000 ได้จากวิกิพีเดีย)

ดูสิว่าครั้งนี้ภาษาไหนจะเขียนได้ผลลัพธ์ก่อน ใครโพสต์คนแรกอย่าลืมโพสต์ผลลัพธ์ให้ดูด้วยนะครับ คนต่อไปถ้าได้ผลลัพธ์เหมือนกัน โพสต์เฉพาะโค้ดก็เพียงพอ

This post has been edited by นายข้าวโพดหวาน: 21 March 2009 - 09:02 PM

0

#2 User is offline   Bleak 

  • Senior member
  • PipPipPip
  • Group: Members
  • Posts: 575
  • Joined: 24-March 05

Posted 21 March 2009 - 01:11 PM

ภาษา C# ครับ เอาเป็น algorithm ง่ายๆ ไม่ซับซ้อน

class Program
{
	const int START = 2;
	const int STOP = 99;
	static void Main(string[] args)
	{
		List<MyPrime> MyPrimes = new List<MyPrime>();
		for (int i = START; i <= STOP; i++)
		{
			List<string> PimeFactors = new List<string>();
			int value = i, index = 0; 
			while (index < MyPrimes.Count && value > 1)
			{
				if (value % MyPrimes[index].Number == 0)
				{
					PimeFactors.Add(MyPrimes[index].Number.ToString());
					value /= MyPrimes[index].Number;
				}
				else
					index++;
			}
			MyPrimes.Add(new MyPrime() { Number = i, PimeFactors = PimeFactors.Count==0 ? new List<string>(){i.ToString()} : PimeFactors });
		}

		foreach (MyPrime m in MyPrimes)
			Console.WriteLine(string.Format("Prime Factors of {0} is [{1}]", m.Number, string.Join("*", m.PimeFactors.ToArray())));
	}
}
class MyPrime
{
	public int Number { get; set; }
	public List<string> PimeFactors { get; set; }
}


ผลลัพธ์

Quote

Prime Factors of 2 is [2]
Prime Factors of 3 is [3]
Prime Factors of 4 is [2*2]
Prime Factors of 5 is [5]
Prime Factors of 6 is [2*3]
Prime Factors of 7 is [7]
Prime Factors of 8 is [2*2*2]
Prime Factors of 9 is [3*3]
Prime Factors of 10 is [2*5]
Prime Factors of 11 is [11]
Prime Factors of 12 is [2*2*3]
Prime Factors of 13 is [13]
Prime Factors of 14 is [2*7]
Prime Factors of 15 is [3*5]
Prime Factors of 16 is [2*2*2*2]
Prime Factors of 17 is [17]
Prime Factors of 18 is [2*3*3]
Prime Factors of 19 is [19]
Prime Factors of 20 is [2*2*5]
Prime Factors of 21 is [3*7]
Prime Factors of 22 is [2*11]
Prime Factors of 23 is [23]
Prime Factors of 24 is [2*2*2*3]
Prime Factors of 25 is [5*5]
Prime Factors of 26 is [2*13]
Prime Factors of 27 is [3*3*3]
Prime Factors of 28 is [2*2*7]
Prime Factors of 29 is [29]
Prime Factors of 30 is [2*3*5]
Prime Factors of 31 is [31]
Prime Factors of 32 is [2*2*2*2*2]
Prime Factors of 33 is [3*11]
Prime Factors of 34 is [2*17]
Prime Factors of 35 is [5*7]
Prime Factors of 36 is [2*2*3*3]
Prime Factors of 37 is [37]
Prime Factors of 38 is [2*19]
Prime Factors of 39 is [3*13]
Prime Factors of 40 is [2*2*2*5]
Prime Factors of 41 is [41]
Prime Factors of 42 is [2*3*7]
Prime Factors of 43 is [43]
Prime Factors of 44 is [2*2*11]
Prime Factors of 45 is [3*3*5]
Prime Factors of 46 is [2*23]
Prime Factors of 47 is [47]
Prime Factors of 48 is [2*2*2*2*3]
Prime Factors of 49 is [7*7]
Prime Factors of 50 is [2*5*5]
Prime Factors of 51 is [3*17]
Prime Factors of 52 is [2*2*13]
Prime Factors of 53 is [53]
Prime Factors of 54 is [2*3*3*3]
Prime Factors of 55 is [5*11]
Prime Factors of 56 is [2*2*2*7]
Prime Factors of 57 is [3*19]
Prime Factors of 58 is [2*29]
Prime Factors of 59 is [59]
Prime Factors of 60 is [2*2*3*5]
Prime Factors of 61 is [61]
Prime Factors of 62 is [2*31]
Prime Factors of 63 is [3*3*7]
Prime Factors of 64 is [2*2*2*2*2*2]
Prime Factors of 65 is [5*13]
Prime Factors of 66 is [2*3*11]
Prime Factors of 67 is [67]
Prime Factors of 68 is [2*2*17]
Prime Factors of 69 is [3*23]
Prime Factors of 70 is [2*5*7]
Prime Factors of 71 is [71]
Prime Factors of 72 is [2*2*2*3*3]
Prime Factors of 73 is [73]
Prime Factors of 74 is [2*37]
Prime Factors of 75 is [3*5*5]
Prime Factors of 76 is [2*2*19]
Prime Factors of 77 is [7*11]
Prime Factors of 78 is [2*3*13]
Prime Factors of 79 is [79]
Prime Factors of 80 is [2*2*2*2*5]
Prime Factors of 81 is [3*3*3*3]
Prime Factors of 82 is [2*41]
Prime Factors of 83 is [83]
Prime Factors of 84 is [2*2*3*7]
Prime Factors of 85 is [5*17]
Prime Factors of 86 is [2*43]
Prime Factors of 87 is [3*29]
Prime Factors of 88 is [2*2*2*11]
Prime Factors of 89 is [89]
Prime Factors of 90 is [2*3*3*5]
Prime Factors of 91 is [7*13]
Prime Factors of 92 is [2*2*23]
Prime Factors of 93 is [3*31]
Prime Factors of 94 is [2*47]
Prime Factors of 95 is [5*19]
Prime Factors of 96 is [2*2*2*2*2*3]
Prime Factors of 97 is [97]
Prime Factors of 98 is [2*7*7]
Prime Factors of 99 is [3*3*11]

This post has been edited by Bleak: 21 March 2009 - 01:23 PM

0

#3 User is offline   pphetra 

  • Star
  • Group: Star
  • Posts: 192
  • Joined: 06-August 04

Posted 21 March 2009 - 01:22 PM

Haskell

เริ่มด้วยการเขียน function compound เพื่อหาตัวประกอบ (เมื่อเป็น functional ก็ต้องใช้ recursive style)
หลักๆก็คือ วนหารเอาเศษ จาก 2 ไปจนถึง square root ของค่าที่ต้องการ
compound n = case first_compound of
			   Nothing -> [n]
			   Just x  -> x : compound( n `div` x )
	where first_compound = find (\x -> (n `mod` x) == 0) [2..target]
		  target = floor $ sqrt $ fromIntegral n

ลอง run ดู
*Main> compound 10
[2,5]
*Main> compound 99
[3,3,11]


ค่าที่ได้มา ยังมี factor ซ้ำๆอยู่ เอาออกด้วยการ group
*Main> group $ compound 99
[[3,3],[11]]


เอาเฉพาะตัวแรกของทุกค่า
*Main> map head $ group $ compound 99
[3,11]


สุดท้ายก็พิมพ์สวยๆ
solve = [(x, (map head . group . compound) x) |  x <- [2..100]]

main = mapM pp solve
	 where pp (x,xs) = putStrLn $ (show x) ++ " -> " ++ (unwords $ map show xs)

0

#4 User is offline   นายข้าวโพดหวาน 

  • Committee
  • View blog
  • Group: Committee
  • Posts: 7075
  • Joined: 01-April 03

Posted 21 March 2009 - 11:25 PM

คราวนี้ C# มาแรงโพสต์ก่อนภาษาอื่นเลย ขอบคุณ Bleak ที่โพสต์ตอบด้วยความรวดเร็ว

View PostBleak, on Mar 21 2009, 01:11 AM, said:

Prime Factors of 40 is [2*2*2*5]


อย่าลืมขั้นตอนสุดท้ายในการเอาเลขซ้ำออก เพราะว่า prime factors คือเซตของ prime number ที่หารตัวเลขลงตัว อย่าง 40 มี prime factors คือ [2,5]
0

#5 User is offline   newjsp 

  • Star
  • View blog
  • Group: Star
  • Posts: 826
  • Joined: 18-May 05

Posted 21 March 2009 - 11:42 PM

groovy ;)
def factors (Double target) {   // return ค่ากลับเป็น list
	def i = 2
	while ( i <= (Integer)(target**0.5)) { // เริ่มจาก 2 จนถึงค่า รากที่สอง
		 if ( target % i == 0 ) {   //ถ้า หารลงตัว 
			 return  factors(i) + factors(target / i)
		 }
		 i++
	 }
	 return [(Integer)target];   
}

// Main Start Here
for (j in 2..99) {
  println "Prime factors of " + j + " : " +   factors(j).unique().sort()	
  // ตัดค่าซ้ำ และเรียงจากน้อยไปมาก
}


Quote

....
Prime factors of 96 : [2, 3]
Prime factors of 97 : [97]
Prime factors of 98 : [2, 7]
Prime factors of 99 : [3, 11]


คำอธิบายเหมือนของคุณ pphetra ครับ :rolleyes:

This post has been edited by newjsp: 21 March 2009 - 11:52 PM

0

#6 User is offline   Revoluter 

  • Star
  • Group: Star
  • Posts: 263
  • Joined: 03-July 05

Posted 22 March 2009 - 12:25 AM

อืม ทำข้อนี้แล้วนึกถึงสาวๆ ตอนเรียนเรื่อง Dynamic & Recursive Programming

import java.util.*;

public class PrimeFactoring
{
	public static void main(String [] args)
	{
		for (int i = 2; i <= 100; i++)
		{
			Set<Integer> result = new LinkedHashSet<Integer>();
			factoring(i, 2, result);
			System.out.println("Prime factors of " + i + " : " + result);
		}
	}
	
	public static void factoring(int number, int factor, Set<Integer> result)
	{
		if (factor == number) result.add(factor);
		else
		{
			if ((number % factor) != 0) factoring(number, factor + 1, result);
			else
			{
				result.add(factor);
				factoring(number/factor, factor, result);
			}
		}
	}
}

0

#7 User is offline   Bleak 

  • Senior member
  • PipPipPip
  • Group: Members
  • Posts: 575
  • Joined: 24-March 05

Posted 22 March 2009 - 09:50 AM

View Postนายข้าวโพดหวาน, on Mar 21 2009, 11:25 PM, said:

คราวนี้ C# มาแรงโพสต์ก่อนภาษาอื่นเลย ขอบคุณ Bleak ที่โพสต์ตอบด้วยความรวดเร็ว

View PostBleak, on Mar 21 2009, 01:11 AM, said:

Prime Factors of 40 is [2*2*2*5]


อย่าลืมขั้นตอนสุดท้ายในการเอาเลขซ้ำออก เพราะว่า prime factors คือเซตของ prime number ที่หารตัวเลขลงตัว อย่าง 40 มี prime factors คือ [2,5]


ฮ้า เข้าใจผิด งั้นแก้ใหม่นิดเดียว
class Program
{
	const int START = 2;
	const int STOP = 99;
	static void Main(string[] args)
	{
		List<MyPrime> MyPrimes = new List<MyPrime>();
		for (int i = START; i <= STOP; i++)
		{
			List<string> PimeFactors = new List<string>();
			int value = i, index = 0;
			while (index < MyPrimes.Count && value > 1)
			{
				if (value % MyPrimes[index].Number == 0)
				{
					PimeFactors.Add(MyPrimes[index].Number.ToString());
					value /= MyPrimes[index].Number;
				}
				else
					index++;
			}
			MyPrimes.Add(new MyPrime() { Number = i, PimeFactors = PimeFactors.Count == 0 ? new List<string>() { i.ToString() } : PimeFactors.Distinct().ToList() });
		}

		foreach (MyPrime m in MyPrimes)
			Console.WriteLine(string.Format("Prime Factors of {0} is [{1}]", m.Number, string.Join(",", m.PimeFactors.ToArray())));
	}
}
class MyPrime
{
	public int Number { get; set; }
	public List<string> PimeFactors { get; set; }
}


Quote

...
Prime Factors of 91 is [7,13]
Prime Factors of 92 is [2,23]
Prime Factors of 93 is [3,31]
Prime Factors of 94 is [2,47]
Prime Factors of 95 is [5,19]
Prime Factors of 96 is [2,3]
Prime Factors of 97 is [97]
Prime Factors of 98 is [2,7]
Prime Factors of 99 is [3,11]


อันนี้เป็น Iterative ว่าจะทำแบบ Recursive มั่ง แต่เหลือบมองข้างบนแล้ว ก็คงไม่ต่างกัน :P

This post has been edited by Bleak: 22 March 2009 - 09:57 AM

0

#8 User is offline   นายข้าวโพดหวาน 

  • Committee
  • View blog
  • Group: Committee
  • Posts: 7075
  • Joined: 01-April 03

Posted 22 March 2009 - 11:51 AM

View Postnewjsp, on Mar 21 2009, 11:42 AM, said:

			 return  factors(i) + factors(target / i)


กรณีนี้ ไม่จำเป็นต้องเรียก factors(i) ครับ ใช้ i + factors(target / i) ได้เลย เพราะ i เรียงจากน้อยไปมาก จะรับประกันว่า i เป็น prime number อยู่แล้ว
0

#9 User is offline   นายข้าวโพดหวาน 

  • Committee
  • View blog
  • Group: Committee
  • Posts: 7075
  • Joined: 01-April 03

Posted 22 March 2009 - 11:58 AM

สำหรับโซลูชั่นภาษารูบี้ มีอัลกอริธึ่มเหมือนของคุณ pphetra ครับ

def find_prime_factors(number)
  (2..Math.sqrt(number).to_i).each do |i|
	return [i] + find_prime_factors(number / i) if number % i == 0
  end
  [number]
end

(2..100).each do |number|
  puts "Prime factors of #{number} is #{find_prime_factors(number).uniq.inspect}"
end


ผมเลือกใช้ do..end แทนที่จะใช้ {} ทำให้โค้ดหลายบรรทัดขึ้น แต่ก็ทำให้อ่านง่ายขึ้นอีกหน่อย และมีข้อสังเกตเพิ่มดังนี้

1. find_prime_factors จะรีเทิร์นแอร์เรย์ของตัวเลขเสมอ ดังนั้นถ้าหาค่าตัวหารลงตัวไม่ได้เลยต้องรีเทิร์นแอร์เรย์ของตัวเลขนั้นด้วย [number]
2. การกำจัดตัวผลลัพธ์ซ้ำในแอร์เรย์ ใช้เมธอด uniq ซึ่งหมายถึง unique
3. แปลงผลรากที่สองจาก floating point ไปเป็น interger ด้วยเมธอด to_i
4. 2..x คือ range จากเลข 2 ไปจนถึงค่า x
0

#10 User is offline   newjsp 

  • Star
  • View blog
  • Group: Star
  • Posts: 826
  • Joined: 18-May 05

Posted 22 March 2009 - 12:22 PM

+1 krup, thank you ;)
0

#11 User is offline   นายข้าวโพดหวาน 

  • Committee
  • View blog
  • Group: Committee
  • Posts: 7075
  • Joined: 01-April 03

Posted 22 March 2009 - 01:14 PM

อันนี้เป็นเวอร์ชั่น scala

def findPrimeFactors(number : Int) : List[Int] = {
  val first_prime_or_none = (2 until Math.sqrt(number)).inclusive.find { i => number % i == 0 }
  if (first_prime_or_none != None) {
	return first_prime_or_none.get :: findPrimeFactors(number / first_prime_or_none.get)
  }
  number :: Nil
}

(2 until 100).inclusive.foreach { 
	number => println(  "Prime factors of " + number + " is " +
						findPrimeFactors(number).removeDuplicates.mkString("[", ", ", "]") )
}


ข้อสังเกต
- scala มี Range m..n ที่เริ่มจาก m ไปจนถึง n-1 ไม่รวม n ถ้าจะให้รวม n ต้องเรียก .inclusive เพิ่ม
- แม้ scala จะเป็นภาษา static type แต่การที่มันสามารถ infer type ได้ทำให้ตัวแปรบางตัวไม่จำเป็นต้องกำหนด type อีก อย่างเช่น first_prime_or_none
- ภาษา static type อย่าง scala ก็สามารถเขียนโซลูชั่นในไม่กี่บรรทัดได้ เพราะความสามารถทาง functional
- ลิสต์ใน scala ใช้ :: ในการเชื่อมแต่ละ member เข้าด้วยกัน และลิสต์จบที่ Nil
- find จะรีเทิร์น Option ถ้าเจอ หรือ None ถ้าไม่เจอ ถ้าเจอแล้วจะดึงค่าที่เจอด้วยเมธอด get
- mkString เป็นเมธอดที่แปลง list ให้เป็นสตริง โดยรับ 3 พารามิเตอร์ คือสตริงเริ่มแรก สตริงที่ใช้คั่นแต่ละ member และสตริงที่ใช้จบ

This post has been edited by นายข้าวโพดหวาน: 22 March 2009 - 01:27 PM

0

#12 User is offline   นายข้าวโพดหวาน 

  • Committee
  • View blog
  • Group: Committee
  • Posts: 7075
  • Joined: 01-April 03

Posted 22 March 2009 - 01:39 PM

ขอปรับปรุงเวอร์ชั่น ruby (ได้ไอเดียจากตอนเขียน scala) โดยตัดส่วนการใช้ for each ไปใช้ find แทน

def find_prime_factors(number)
  first_prime_number = (2..Math.sqrt(number).to_i).find { |i| number % i == 0 }
  return [number] if first_prime_number.nil?
  [first_prime_number] + find_prime_factors(number / first_prime_number)
end

(2..100).each do |number|
  puts "Prime factors of #{number} is #{find_prime_factors(number).uniq.inspect}"
end

This post has been edited by นายข้าวโพดหวาน: 22 March 2009 - 01:45 PM

0

#13 User is offline   m3rlinez 

  • Newbie
  • Pip
  • Group: Members
  • Posts: 42
  • Joined: 01-October 06

Posted 22 March 2009 - 07:45 PM

ผมลอง port code ของพี่ข้าวโพดหวาน จาก scala มาเป็น C# 3.5 ลองเทียบกันดูเล่นๆครับ :)

Scala:
def findPrimeFactors(number : Int) : List[Int] = {
  val first_prime_or_none = (2 until Math.sqrt(number)).inclusive.find { i => number % i == 0 }
  if (first_prime_or_none != None) {
	return first_prime_or_none.get :: findPrimeFactors(number / first_prime_or_none.get)
  }
  number :: Nil
}

(2 until 100).inclusive.foreach { 
	number => println(  "Prime factors of " + number + " is " +
						findPrimeFactors(number).removeDuplicates.mkString("[", ", ", "]") )
}


C# 3.5:
static void Main(string[] args)
{
	for (int number = 2; number <= 99; number++)
		Console.WriteLine("Prime factors of {0} is [{1}]",
				number, String.Join(", ", FindPrimeFactors(number)
					.ToList().ConvertAll<string>(x => x.ToString())
					.ToArray())
				);
}

static HashSet<int> FindPrimeFactors(int number)
{
	var firstPrimeOrNone = Enumerable.Range(2, (int)Math.Ceiling(Math.Sqrt(number)))
		.ToList().Find(x => number % x == 0);

	if (firstPrimeOrNone != 0)
	{
		var ret = FindPrimeFactors(number / firstPrimeOrNone);
		ret.Add(firstPrimeOrNone);
		return ret;
	}

	return new HashSet<int>(new int[] { number });;
}


พยายามเอา element ที่ซ้ำออกโดยใช้ HashSet ผลลัพธ์ (factors) มันก็เลยไม่เรียงตามลำดับน่ะครับ

ปล. ผมลบส่วน Using, Class, และ NameSpace ออก จะได้เทียบได้ง่ายๆ

This post has been edited by m3rlinez: 22 March 2009 - 07:48 PM

0

#14 User is offline   nuboat 

  • Star
  • Group: Star
  • Posts: 1654
  • Joined: 11-October 03

Posted 22 March 2009 - 09:57 PM

เป็น python 2.6.1 นะครับ

__author__="nuboat"
__date__ ="$22 03 2552 20:30:00$"

list_prime = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97]

def findfactor(data, list_result):
    if data < 2:
        return None
    
    for i in list_prime:
        if (data % i) == 0:
            list_result.append(i)
            findfactor( data/i, list_result)
            break;

if __name__ == "__main__":
    for x in xrange(100):
        list_result = []
        findfactor(x, list_result)

        if len(list_result) != 0:
            print "Factor of " + str(x) + " is " + str(set(list_result))
        else:
            print "Factor of " + str(x) + " is " + str([].append(x))


โค้ดรอบสองแก้ส่วนไร้สาระออกเฉยๆ
list_prime = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97]

def findfactor(data, list_result):
    for i in [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97]:
        if (data % i) == 0:
            list_result.append(i)
            findfactor( data/i, list_result)

if __name__ == "__main__":
    for x in xrange(2,100):
        list_result = []
        findfactor(x, list_result)
        print len(list_result)!=0 and "Factor of " + str(x) + " is " + str(set(list_result)) or "Factor of " + str(x) + " is " + str([].append(x));


Factor of 0 is None
Factor of 1 is None
Factor of 2 is set([2])
Factor of 3 is set([3])
Factor of 4 is set([2])
Factor of 5 is set([5])
Factor of 6 is set([2, 3])
Factor of 7 is set([7])
Factor of 8 is set([2])
Factor of 9 is set([3])
Factor of 10 is set([2, 5])
Factor of 11 is set([11])
Factor of 12 is set([2, 3])
Factor of 13 is set([13])
Factor of 14 is set([2, 7])
Factor of 15 is set([3, 5])
Factor of 16 is set([2])
Factor of 17 is set([17])
Factor of 18 is set([2, 3])
Factor of 19 is set([19])
Factor of 20 is set([2, 5])
Factor of 21 is set([3, 7])
Factor of 22 is set([2, 11])
Factor of 23 is set([23])
Factor of 24 is set([2, 3])
Factor of 25 is set([5])
Factor of 26 is set([2, 13])
Factor of 27 is set([3])
Factor of 28 is set([2, 7])
Factor of 29 is set([29])
Factor of 30 is set([2, 3, 5])
Factor of 31 is set([31])
Factor of 32 is set([2])
Factor of 33 is set([11, 3])
Factor of 34 is set([17, 2])
Factor of 35 is set([5, 7])
Factor of 36 is set([2, 3])
Factor of 37 is set([37])
Factor of 38 is set([2, 19])
Factor of 39 is set([3, 13])
Factor of 40 is set([2, 5])
Factor of 41 is set([41])
Factor of 42 is set([2, 3, 7])
Factor of 43 is set([43])
Factor of 44 is set([2, 11])
Factor of 45 is set([3, 5])
Factor of 46 is set([2, 23])
Factor of 47 is set([47])
Factor of 48 is set([2, 3])
Factor of 49 is set([7])
Factor of 50 is set([2, 5])
Factor of 51 is set([17, 3])
Factor of 52 is set([2, 13])
Factor of 53 is set([53])
Factor of 54 is set([2, 3])
Factor of 55 is set([11, 5])
Factor of 56 is set([2, 7])
Factor of 57 is set([19, 3])
Factor of 58 is set([2, 29])
Factor of 59 is set([59])
Factor of 60 is set([2, 3, 5])
Factor of 61 is set([61])
Factor of 62 is set([2, 31])
Factor of 63 is set([3, 7])
Factor of 64 is set([2])
Factor of 65 is set([5, 13])
Factor of 66 is set([11, 2, 3])
Factor of 67 is set([67])
Factor of 68 is set([17, 2])
Factor of 69 is set([3, 23])
Factor of 70 is set([2, 5, 7])
Factor of 71 is set([71])
Factor of 72 is set([2, 3])
Factor of 73 is set([73])
Factor of 74 is set([2, 37])
Factor of 75 is set([3, 5])
Factor of 76 is set([2, 19])
Factor of 77 is set([11, 7])
Factor of 78 is set([2, 3, 13])
Factor of 79 is set([79])
Factor of 80 is set([2, 5])
Factor of 81 is set([3])
Factor of 82 is set([41, 2])
Factor of 83 is set([83])
Factor of 84 is set([2, 3, 7])
Factor of 85 is set([17, 5])
Factor of 86 is set([2, 43])
Factor of 87 is set([3, 29])
Factor of 88 is set([2, 11])
Factor of 89 is set([89])
Factor of 90 is set([2, 3, 5])
Factor of 91 is set([13, 7])
Factor of 92 is set([2, 23])
Factor of 93 is set([3, 31])
Factor of 94 is set([2, 47])
Factor of 95 is set([19, 5])
Factor of 96 is set([2, 3])
Factor of 97 is set([97])
Factor of 98 is set([2, 7])
Factor of 99 is set([11, 3])

This post has been edited by aiboat: 24 March 2009 - 09:25 PM

0

#15 User is offline   roticagas 

  • Newbie
  • Pip
  • Group: Members
  • Posts: 3
  • Joined: 23-November 04

Posted 24 March 2009 - 09:02 PM

เห็นว่าเขียนบรรทัดเดียวได้เลยลงภาษาซ้ำน่ะครับ (แต่ไม่ได้อ่านง่ายเท่าไหร่เลย)

python 2.5.1

print '\n'.join( [ 'Factor of %i is %s' %( n, [ i for i in [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47] if n % i == 0 ] == [] and str([n]) or str([ i for i in [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47] if n % i == 0 ]) ) for n in range(2,100) ] )

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users