Advertisement
nguyenvanquan7826

KTPM K10

Nov 17th, 2014
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.29 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace So
  8. {
  9.     class Program
  10.     {
  11.  
  12.     /*
  13.     ham kiem tra mot so co phai la so nguyen to khong.
  14.     so nguyen to la so chi co 2 uoc la 1 va chinh no (2, 3, 5, 7, ...)
  15.     */
  16.         public static bool ktNT(int n)
  17.         {
  18.  
  19.             if (n < 2)
  20.             {
  21.                 return false;
  22.             }
  23.  
  24.             for (int i = 2; i <= Math.Sqrt(n); i++ )
  25.             {
  26.                 if (n % i == 0)
  27.                 {
  28.                     return false;
  29.                 }
  30.             }
  31.  
  32.             return true;
  33.         }
  34.  
  35.     /*
  36.     ham kiem tra mot so co phai la so chinh phuong khong.
  37.     so chinh phuong la so chi co can bac 2 cua no la mot so nguyen (4, 9, 16, ...)
  38.     */
  39.         public static bool ktCP(int n)
  40.         {
  41.             double can = Math.Sqrt(n);
  42.  
  43.             if (can == (int)can)
  44.             {
  45.                 return true;
  46.             }
  47.             return false;
  48.         }
  49.  
  50.  
  51.     /*
  52.     ham kiem tra mot so co phai la so hoan hao khong.
  53.     so hoan hao la so co tong cac uoc nho hon no bang chinh no (6, 28, ...)
  54.     */
  55.         public static bool ktHH(int n)
  56.         {
  57.             int tong = 0;
  58.            
  59.             for (int i = 1; i < n; i++)
  60.             {
  61.                 if (n % i == 0)
  62.                 {
  63.                     tong = tong + i;
  64.                 }
  65.             }
  66.  
  67.             if (tong == n)
  68.             {
  69.                 return true;
  70.             }
  71.             return false;
  72.         }
  73.  
  74.  
  75.     /*
  76.     ham tinh tong cac so nguyen to trong khoang tu a den b.
  77.     */
  78.         public static  int tongNT(int a, int b)
  79.         {
  80.             int tong = 0;
  81.             for (int i = a; i <= b; i++)
  82.             {
  83.                 if (ktNT(i))
  84.                 {
  85.                     tong = tong + i;
  86.                 }
  87.             }
  88.             return tong;
  89.         }
  90.  
  91.         static void Main(string[] args)
  92.         {
  93.             Console.WriteLine("Nhap 2 so ban can kiem tra: ");
  94.             int a = Convert.ToInt16(Console.ReadLine());
  95.             int b = Convert.ToInt16(Console.ReadLine());
  96.  
  97.             Console.WriteLine("Tong cac so nguyen to tu a den b la: " + tongNT(a, b));
  98.  
  99.             Console.ReadLine();
  100.         }
  101.        
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement