Skip to content

Commit 0e27a89

Browse files
authored
Merge pull request ephremdeme#52 from gauravmadan583/master
Optimizing isPrime in C++
2 parents cf0ab95 + e2453d5 commit 0e27a89

File tree

4 files changed

+73
-10
lines changed

4 files changed

+73
-10
lines changed

Diff for: .vscode/launch.json

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "g++.exe - Build and debug active file",
9+
"type": "cppdbg",
10+
"request": "launch",
11+
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
12+
"args": [],
13+
"stopAtEntry": false,
14+
"cwd": "${workspaceFolder}",
15+
"environment": [],
16+
"externalConsole": false,
17+
"MIMode": "gdb",
18+
"miDebuggerPath": "C:\\MinGW\\bin\\gdb.exe",
19+
"setupCommands": [
20+
{
21+
"description": "Enable pretty-printing for gdb",
22+
"text": "-enable-pretty-printing",
23+
"ignoreFailures": true
24+
}
25+
],
26+
"preLaunchTask": "C/C++: g++.exe build active file"
27+
}
28+
]
29+
}

Diff for: .vscode/tasks.json

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"tasks": [
3+
{
4+
"type": "shell",
5+
"label": "C/C++: g++.exe build active file",
6+
"command": "C:\\MinGW\\bin\\g++.exe",
7+
"args": [
8+
"-g",
9+
"${file}",
10+
"-o",
11+
"${fileDirname}\\${fileBasenameNoExtension}.exe"
12+
],
13+
"options": {
14+
"cwd": "${workspaceFolder}"
15+
},
16+
"problemMatcher": [
17+
"$gcc"
18+
],
19+
"group": {
20+
"kind": "build",
21+
"isDefault": true
22+
}
23+
}
24+
],
25+
"version": "2.0.0"
26+
}

Diff for: problems/isPrime.cpp

+18-10
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,29 @@ using namespace std;
33

44
int main()
55
{
6+
ios_base::sync_with_stdio(false);
7+
cin.tie(NULL);
8+
cout.tie(NULL);
9+
610
int n;
711
cin>>n;
8-
int d=2;
912
bool divided=false;
10-
while(d<n)
11-
{
12-
if(n%d==0)
13-
{
14-
cout<<"False"<<endl;
15-
divided=true;
13+
if(n%2==0){
14+
divided=true;
15+
}
16+
else{
17+
int d=3;
18+
while(d*d<n){
19+
if(n%d==0){
20+
divided=true;
21+
break;
22+
}
23+
d+=2;
1624
}
17-
d++;
1825
}
19-
if(!divided)
20-
{
26+
if(!divided){
2127
cout<<"True"<<endl;
28+
}else{
29+
cout<<"False"<<endl;
2230
}
2331
}

Diff for: problems/isPrime.exe

102 KB
Binary file not shown.

0 commit comments

Comments
 (0)