Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace So
- {
- class Program
- {
- /*
- ham kiem tra mot so co phai la so nguyen to khong.
- so nguyen to la so chi co 2 uoc la 1 va chinh no (2, 3, 5, 7, ...)
- */
- public static bool ktNT(int n)
- {
- if (n < 2)
- {
- return false;
- }
- for (int i = 2; i <= Math.Sqrt(n); i++ )
- {
- if (n % i == 0)
- {
- return false;
- }
- }
- return true;
- }
- /*
- ham kiem tra mot so co phai la so chinh phuong khong.
- so chinh phuong la so chi co can bac 2 cua no la mot so nguyen (4, 9, 16, ...)
- */
- public static bool ktCP(int n)
- {
- double can = Math.Sqrt(n);
- if (can == (int)can)
- {
- return true;
- }
- return false;
- }
- /*
- ham kiem tra mot so co phai la so hoan hao khong.
- so hoan hao la so co tong cac uoc nho hon no bang chinh no (6, 28, ...)
- */
- public static bool ktHH(int n)
- {
- int tong = 0;
- for (int i = 1; i < n; i++)
- {
- if (n % i == 0)
- {
- tong = tong + i;
- }
- }
- if (tong == n)
- {
- return true;
- }
- return false;
- }
- /*
- ham tinh tong cac so nguyen to trong khoang tu a den b.
- */
- public static int tongNT(int a, int b)
- {
- int tong = 0;
- for (int i = a; i <= b; i++)
- {
- if (ktNT(i))
- {
- tong = tong + i;
- }
- }
- return tong;
- }
- static void Main(string[] args)
- {
- Console.WriteLine("Nhap 2 so ban can kiem tra: ");
- int a = Convert.ToInt16(Console.ReadLine());
- int b = Convert.ToInt16(Console.ReadLine());
- Console.WriteLine("Tong cac so nguyen to tu a den b la: " + tongNT(a, b));
- Console.ReadLine();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement