Skip to content

Commit

Permalink
feat (prime exercise): resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
santanche committed Mar 23, 2021
1 parent 064cc78 commit d8b5ab6
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 119 deletions.
120 changes: 1 addition & 119 deletions notebook/pt/c51oo/s01primos/s01exercicios/primos01.ipynb
Original file line number Diff line number Diff line change
@@ -1,119 +1 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "central-briefing",
"metadata": {},
"source": [
"# Números Primos"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "senior-stack",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Este numero eh primo"
]
}
],
"source": [
"#include <stdio.h>\n",
"\n",
"int main() {\n",
" int numero = 7,\n",
" flag=0;\n",
"\n",
" for (int i = 2; i < numero; i++)\n",
" if (numero % i == 0)\n",
" flag = 1;\n",
"\n",
" if (flag == 0)\n",
" printf(\"Este numero eh primo\");\n",
" else\n",
" printf(\"Não eh primo\");\n",
"\n",
"\n",
" return 0;\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "incoming-defeat",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n",
"3\n",
"5\n",
"7\n",
"11\n",
"13\n",
"17\n",
"19\n",
"23\n"
]
}
],
"source": [
"#include <stdio.h>\n",
"\n",
"int main() {\n",
" int n = 10,\n",
" flag;\n",
"\n",
" int numero = 1;\n",
" int t = 0;\n",
" while (t < n) {\n",
" flag = 0;\n",
" for (int i = 2; i < numero; i++)\n",
" if (numero % i == 0)\n",
" flag = 1;\n",
"\n",
" if (flag == 0) {\n",
" printf(\"%d\\n\", numero);\n",
" t++;\n",
" }\n",
" \n",
" numero++;\n",
" }\n",
"\n",
" return 0;\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "accurate-hometown",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "C",
"language": "c",
"name": "c"
},
"language_info": {
"file_extension": ".c",
"mimetype": "text/plain",
"name": "c"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
{"metadata":{"kernelspec":{"display_name":"C","language":"c","name":"c"},"language_info":{"file_extension":".c","mimetype":"text/plain","name":"c"}},"nbformat_minor":5,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Números Primos","metadata":{}},{"cell_type":"code","source":"#include <stdio.h>\n\nint main() {\n int numero = 7,\n flag=0;\n\n for (int i = 2; i < numero; i++)\n if (numero % i == 0)\n flag = 1;\n\n if (flag == 0)\n printf(\"Este numero eh primo\");\n else\n printf(\"Não eh primo\");\n\n\n return 0;\n}","metadata":{},"execution_count":3,"outputs":[{"name":"stdout","output_type":"stream","text":"Este numero eh primo"}]},{"cell_type":"code","source":"#include <stdio.h>\n\nint main() {\n int n = 10,\n flag;\n\n int numero = 1;\n int t = 0;\n while (t < n) {\n flag = 0;\n for (int i = 2; i < numero; i++)\n if (numero % i == 0)\n flag = 1;\n\n if (flag == 0) {\n printf(\"%d\\n\", numero);\n t++;\n }\n \n numero++;\n }\n\n return 0;\n}","metadata":{},"execution_count":2,"outputs":[{"name":"stdout","output_type":"stream","text":"1\n2\n3\n5\n7\n11\n13\n17\n19\n23\n"}]},{"cell_type":"markdown","source":"# Exercício\n\nConstrua um gerador de números primos seguindo a lógica de um Tipo Abstrato de Dados.","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"metadata":{"kernelspec":{"display_name":"C","language":"c","name":"c"},"language_info":{"file_extension":".c","mimetype":"text/plain","name":"c"}},"nbformat_minor":5,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Números Primos","metadata":{}},{"cell_type":"code","source":"#include <stdio.h>\n\nint main() {\n int numero = 4,\n flag=0;\n\n for (int i = 2; i < numero; i++)\n if (numero % i == 0)\n flag = 1;\n\n if (flag == 0)\n printf(\"Este numero eh primo\");\n else\n printf(\"Não eh primo\");\n\n\n return 0;\n}","metadata":{"trusted":true},"execution_count":2,"outputs":[{"name":"stdout","text":"Não eh primo","output_type":"stream"}]},{"cell_type":"code","source":"#include <stdio.h>\n\nint main() {\n int n = 10,\n flag;\n\n int numero = 1;\n int t = 0;\n while (t < n) {\n flag = 0;\n for (int i = 2; i < numero; i++)\n if (numero % i == 0)\n flag = 1;\n\n if (flag == 0) {\n printf(\"%d\\n\", numero);\n t++;\n }\n \n numero++;\n }\n\n return 0;\n}","metadata":{"trusted":true},"execution_count":5,"outputs":[{"name":"stdout","text":"1\n2\n3\n5\n7\n11\n13\n17\n19\n23\n","output_type":"stream"}]},{"cell_type":"markdown","source":"# Exercício\n\nConstrua um gerador de números primos seguindo a lógica de um Tipo Abstrato de Dados.","metadata":{}},{"cell_type":"code","source":"#include <stdio.h>\n\ntypedef struct {\n int n;\n int corrente;\n int primoCorrente;\n} SequenciaPrimos;\n\nSequenciaPrimos novaSequencia(int n) {\n SequenciaPrimos umaSequencia;\n umaSequencia.n = n;\n umaSequencia.corrente = 0;\n umaSequencia.primoCorrente = 0;\n return umaSequencia;\n}\n\nint testaPrimo(int numero) {\n int flag = 1;\n for (int i = 2; i < numero; i++)\n if (numero % i == 0)\n flag = 0;\n return flag;\n}\n\nint proximoPrimo(SequenciaPrimos *sequencia) {\n int retorno = 0;\n if (sequencia->corrente < sequencia->n) {\n sequencia->corrente++;\n int flag;\n do {\n sequencia->primoCorrente++;\n flag = testaPrimo(sequencia->primoCorrente);\n } while (flag == 0);\n retorno = sequencia->primoCorrente;\n }\n return retorno;\n}\n\nint main() {\n SequenciaPrimos sequencia = novaSequencia(10);\n\n int primo = proximoPrimo(&sequencia);\n \n while (primo > 0) {\n printf(\"%d\\n\", primo);\n primo = proximoPrimo(&sequencia);\n }\n\n return 0;\n}","metadata":{"trusted":true},"execution_count":4,"outputs":[{"name":"stdout","text":"1\n2\n3\n5\n7\n11\n13\n17\n19\n23\n","output_type":"stream"}]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}

0 comments on commit d8b5ab6

Please sign in to comment.