You are on page 1of 6

BT4-5. a.Cho tng S=1+3+5+...+(2n+1) vi n0 v s nguyn M. Hy tm gi tr n nh nht sao cho S >M. #include<stdio.h> #include<conio.

h> void main() { int i,s,n,m,min; printf("nhap n="); scanf("%d",&n); printf("nhap so nguyen m="); scanf("%d",&m); i=0; s=1; while(i<n) { s=s+(i+i+1); i=i+i+1; } printf("tong=%d",s); if(s>m) { min=n<m?n:m; printf("gia tri n nho nhat la=%d",min); } else printf("ban nen chon s>m"); getch(); } b. Nhp s nguyn t n. Hy tim s nguyn t k tip sau s n. #include<conio.h> #include<iostream.h> void main() { clrscr(); int n; cout<<"Nhap vao so nguyen to n = ";cin>>n; int flag=1,i=n+1,d; if (n==2) cout<<"So nguyen to can tim la : "<<3; else while (flag) { int d=0; for (int j=1;j<=i;j++) if (i%j==0) d++;

if (d==2) { cout<<"So nguyen to can tim la : "<<i; flag=0; } i=i+1; } getch(); } c.Nhp vo s nguyn n. Hy tm s nguyn t gn n nht. #include<conio.h> #include<iostream.h> void main() { clrscr(); int n; cout<<"Nhap vao so nguyen n = ";cin>>n; int flag=1,i=n+1,d; if (n==2) cout<<"So nguyen to can tim la : "<<3; else while (flag) { int d=0; for (int j=1;j<=i;j++) if (i%j==0) d++; if (d==2) { cout<<"So nguyen to can tim la : "<<i; flag=0; } i=i+1; } getch(); } BT4-9.Ta c 3,5,7 l cp s cng bao gm cc s nguyn t. Hy kim tra xem trong cc s t nhin <10000 cn c mt cp s cng no khc gm 3 phn t l s nguyn t na hay khng ? // SoNguyenTo.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include "math.h"

// ta co 430 so nguyen to trong khoang 1-3000 #define MAX_ARRAY_LEN 500 bool IsPrime(int); bool BelongToArray(const int*, int); int main(int argc, char* argv[]) { printf("Hello World!\n"); // khai bao mang chua cac so nguyen to int* A = new int[MAX_ARRAY_LEN]; if (A == NULL) { printf("Khong du bo nho\n"); return 0; } // khoi tao gia tri cho mang A // mang A chua 430 so nguyen to trong khoang 1-3000 // bien lenOfA se la 429 int lenOfA = 0; for (int x = 2; x < 3000; x++) { if (IsPrime(x)) { A[lenOfA] = x; lenOfA++; } } // tim cac cap so cong nam thoa dieu kien la so nguyen to nam trong 1-3000 // cach tim dua theo http://manguon.com/forums/thread/445408.aspx // khai bao cac bien int beginItem, endItem, nextItem, constProgress, countOfProgress; bool canGetNextItem = true; for ( int i = 0; i < lenOfA; i++) { for (int j = i + 1; j < lenOfA; j++) { // gan lai cac bien truoc khi vao vong lap beginItem = A[ i ]; nextItem = A[ j ];

endItem = 3000; constProgress = nextItem - beginItem; canGetNextItem = true; countOfProgress = 0; // vong lap tim ra cap so cong while(canGetNextItem) { if (BelongToArray(A, nextItem)) { countOfProgress++; nextItem += constProgress; if (nextItem > endItem) { endItem = nextItem - constProgress; canGetNextItem = false; } } else { endItem = nextItem - constProgress; canGetNextItem = false; } } // kiem tra neu cap so co nhieu hon k (trong TH nay > 7) phan tu thi hien thi // thong tin cua cap so: pt_bat_dau, pt_ket_thuc, co bao nhieu pt, cong boi if (countOfProgress > 7) { printf("ptBD: %d; ptKT: %d; CB: %d; soPT%d\n", beginItem, endItem, constProgress, countOfProgress); } } } return 0; getch(); } // Kiem tra xem so nguyen x co nam trong mang A hay khong // dung de kiem tra phan tu ke tiep trong cap so co thoa dk // la so nguyen to thuoc khoang 1-3000 hay khong // do mang ban dau da co thu tu, nen khuyen khich cac ban dung // BinarySearch de cho nhanh hon, hien tai toi dung tim tuyen tinh

bool BelongToArray(const int* A, int x) { for (int i = 0; i < MAX_ARRAY_LEN; i++) { if (A[ i ] > x) { return false; } if (A[ i ] == x) return true; } return false; } // Kiem tra so nguyen to thong thuong // theo cach kiem tra tu 2 den can bac hai cua so do // khong co so nao la uoc so cua so can kiem tra bool IsPrime(int number) { if (number < 2) { return false; } int maxCount = 1 + (int)sqrt(number); for (int i = 2; i < maxCount; i++) { if (number % i == 0) return false; } return true;

BT5-3.Hy phn tch s t nhin n thnh tch cc s nguyn t V d 90=2 * 3 3 * 5. #include<iostream.h> #include<conio.h> void main() {

int n; cout<<"nhap n:"; cin>>n; int i=2,j=0; cout<<"1"; while(n>=i) { if(n%i==0) { n=n/i; j=j+1; if(n%i!=0) { if(j>1) cout<<"*"<<i<<"^"<<j; else cout<<"*"<<i; } } else { j=0; i=i+1; } } cout<<endl; getch(); }

You might also like