Skip to content

Commit 489f27c

Browse files
committed
first commit
0 parents commit 489f27c

File tree

5 files changed

+361
-0
lines changed

5 files changed

+361
-0
lines changed

.gitignore

Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
2+
### Eclipse ###
3+
.metadata
4+
bin/
5+
tmp/
6+
*.tmp
7+
*.bak
8+
*.swp
9+
*~.nib
10+
local.properties
11+
.settings/
12+
.loadpath
13+
.recommenders
14+
15+
# External tool builders
16+
.externalToolBuilders/
17+
18+
# Locally stored "Eclipse launch configurations"
19+
*.launch
20+
21+
# PyDev specific (Python IDE for Eclipse)
22+
*.pydevproject
23+
24+
# CDT-specific (C/C++ Development Tooling)
25+
.cproject
26+
27+
# CDT- autotools
28+
.autotools
29+
30+
# Java annotation processor (APT)
31+
.factorypath
32+
33+
# PDT-specific (PHP Development Tools)
34+
.buildpath
35+
36+
# sbteclipse plugin
37+
.target
38+
39+
# Tern plugin
40+
.tern-project
41+
42+
# TeXlipse plugin
43+
.texlipse
44+
45+
# STS (Spring Tool Suite)
46+
.springBeans
47+
48+
# Code Recommenders
49+
.recommenders/
50+
51+
# Annotation Processing
52+
.apt_generated/
53+
.apt_generated_test/
54+
55+
# Scala IDE specific (Scala & Java development for Eclipse)
56+
.cache-main
57+
.scala_dependencies
58+
.worksheet
59+
60+
# Uncomment this line if you wish to ignore the project description file.
61+
# Typically, this file would be tracked if it contains build/dependency configurations:
62+
#.project
63+
64+
### Eclipse Patch ###
65+
# Spring Boot Tooling
66+
.sts4-cache/
67+
68+
### Intellij+all ###
69+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
70+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
71+
72+
# User-specific stuff
73+
.idea
74+
.idea/**/workspace.xml
75+
.idea/**/tasks.xml
76+
.idea/**/usage.statistics.xml
77+
.idea/**/dictionaries
78+
.idea/**/shelf
79+
80+
# AWS User-specific
81+
.idea/**/aws.xml
82+
83+
# Generated files
84+
.idea/**/contentModel.xml
85+
86+
# Sensitive or high-churn files
87+
.idea/**/dataSources/
88+
.idea/**/dataSources.ids
89+
.idea/**/dataSources.local.xml
90+
.idea/**/sqlDataSources.xml
91+
.idea/**/dynamic.xml
92+
.idea/**/uiDesigner.xml
93+
.idea/**/dbnavigator.xml
94+
95+
# Gradle
96+
.idea/**/gradle.xml
97+
.idea/**/libraries
98+
99+
# Gradle and Maven with auto-import
100+
# When using Gradle or Maven with auto-import, you should exclude module files,
101+
# since they will be recreated, and may cause churn. Uncomment if using
102+
# auto-import.
103+
# .idea/artifacts
104+
# .idea/compiler.xml
105+
# .idea/jarRepositories.xml
106+
# .idea/modules.xml
107+
# .idea/*.iml
108+
# .idea/modules
109+
*.iml
110+
111+
# *.ipr
112+
113+
# CMake
114+
cmake-build-*/
115+
116+
# Mongo Explorer plugin
117+
.idea/**/mongoSettings.xml
118+
119+
# File-based project format
120+
*.iws
121+
122+
# IntelliJ
123+
out/
124+
125+
# mpeltonen/sbt-idea plugin
126+
.idea_modules/
127+
128+
# JIRA plugin
129+
atlassian-ide-plugin.xml
130+
131+
# Cursive Clojure plugin
132+
.idea/replstate.xml
133+
134+
# SonarLint plugin
135+
.idea/sonarlint/
136+
137+
# Crashlytics plugin (for Android Studio and IntelliJ)
138+
com_crashlytics_export_strings.xml
139+
crashlytics.properties
140+
crashlytics-build.properties
141+
fabric.properties
142+
143+
# Editor-based Rest Client
144+
.idea/httpRequests
145+
146+
# Android studio 3.1+ serialized cache file
147+
.idea/caches/build_file_checksums.ser
148+
149+
### Intellij+all Patch ###
150+
# Ignore everything but code style settings and run configurations
151+
# that are supposed to be shared within teams.
152+
153+
.idea/*
154+
155+
!.idea/codeStyles
156+
!.idea/runConfigurations
157+
158+
### Java ###
159+
# Compiled class file
160+
*.class
161+
162+
# Log file
163+
*.log
164+
165+
# BlueJ files
166+
*.ctxt
167+
168+
# Mobile Tools for Java (J2ME)
169+
.mtj.tmp/
170+
171+
# Package Files #
172+
*.jar
173+
*.war
174+
*.nar
175+
*.ear
176+
*.zip
177+
*.tar.gz
178+
*.rar
179+
180+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
181+
hs_err_pid*
182+
replay_pid*
183+
184+
### Maven ###
185+
target/
186+
pom.xml.tag
187+
pom.xml.releaseBackup
188+
pom.xml.versionsBackup
189+
pom.xml.next
190+
release.properties
191+
dependency-reduced-pom.xml
192+
buildNumber.properties
193+
.mvn/timing.properties
194+
# https://github.com/takari/maven-wrapper#usage-without-binary-jar
195+
.mvn/wrapper/maven-wrapper.jar
196+
197+
# Eclipse m2e generated files
198+
# Eclipse Core
199+
.project
200+
# JDT-specific (Eclipse Java Development Tools)
201+
.classpath
202+
203+
### VisualStudioCode ###
204+
.vscode/*
205+
!.vscode/settings.json
206+
!.vscode/tasks.json
207+
!.vscode/launch.json
208+
!.vscode/extensions.json
209+
!.vscode/*.code-snippets
210+
211+
# Local History for Visual Studio Code
212+
.history/
213+
214+
# Built Visual Studio Code Extensions
215+
*.vsix
216+
217+
### VisualStudioCode Patch ###
218+
# Ignore all local history of files
219+
.history
220+
.ionide
221+

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Recursión en Java
2+
3+
Resolución de problemas utilizando enfoques:
4+
5+
* Iterativo
6+
* Recursivo
7+
* Recursivo de cola

pom.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.example</groupId>
8+
<artifactId>java-recursion</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<maven.compiler.source>18</maven.compiler.source>
13+
<maven.compiler.target>18</maven.compiler.target>
14+
</properties>
15+
16+
</project>
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.example;
2+
3+
import java.time.Duration;
4+
import java.time.Instant;
5+
6+
public class Factorial {
7+
8+
public static void main(String[] args) {
9+
Long number = 20L;
10+
long start = System.currentTimeMillis();
11+
long result = factorialIterative(number);
12+
long end = System.currentTimeMillis();
13+
System.out.println("Tiempo: " + (end - start) + ", Resultado: " + result);
14+
15+
number = 20L;
16+
start = System.currentTimeMillis();
17+
result = factorialRecursive(number);
18+
end = System.currentTimeMillis();
19+
System.out.println("Tiempo: " + (end - start) + ", Resultado: " + result);
20+
21+
number = 20L;
22+
start = System.currentTimeMillis();
23+
result = factorialRecursiveTail(number);
24+
end = System.currentTimeMillis();
25+
System.out.println("Tiempo: " + (end - start) + ", Resultado: " + result);
26+
27+
}
28+
29+
static long factorialIterative(Long num){
30+
long result = 1;
31+
for(int i = 1; i<=num; i++)
32+
result *= i;
33+
34+
return result;
35+
}
36+
37+
static long factorialRecursive(Long num){
38+
if(num == 1)
39+
return 1;
40+
return num * factorialRecursive(num - 1);
41+
}
42+
43+
// Versión mejorada de la recusión: recursión de cola
44+
45+
static long factorialRecursiveTail(Long num){
46+
return factorialHelper(1, num);
47+
}
48+
49+
static long factorialHelper(long acumulador, Long num){
50+
if (num == 1)
51+
return acumulador;
52+
return factorialHelper(acumulador * num, num - 1);
53+
}
54+
55+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.example;
2+
3+
public class Reverse {
4+
5+
public static void main(String[] args) {
6+
String text = "ABC";
7+
8+
String reversed = reverseIterative(text);
9+
System.out.println(reversed);
10+
11+
reversed = reverseRecursive(text);
12+
System.out.println(reversed);
13+
14+
reversed = reverseRecursiveTail(text, "");
15+
System.out.println(reversed);
16+
}
17+
18+
19+
20+
21+
private static String reverseIterative(String text) {
22+
23+
byte[] textBytes = text.getBytes();
24+
byte[] result = new byte[textBytes.length];
25+
26+
for(int i = 0; i < textBytes.length; i++){
27+
result[i] = textBytes[textBytes.length - i - 1];
28+
}
29+
30+
return new String(result);
31+
}
32+
33+
/*
34+
reverseRecursive("ABC")
35+
reverseRecursive("BC") + 'A'
36+
(reverseRecursive("C") + 'B') + 'A'
37+
("C" + 'B') + 'A'
38+
"CB" + 'A'
39+
"CBA"
40+
41+
*/
42+
private static String reverseRecursive(String text) {
43+
if(text.isEmpty())
44+
return text;
45+
return reverseRecursive(text.substring(1)) + text.charAt(0);
46+
}
47+
48+
// recursión mejorada
49+
/*
50+
reverseRecursiveTail("ABC", "")
51+
reverseRecursiveTail("BC", "A")
52+
reverseRecursiveTail("C", "BA")
53+
reverseRecursiveTail("", "CBA")
54+
"CBA"
55+
*/
56+
private static String reverseRecursiveTail(String text, String acumulador) {
57+
if(text.isEmpty())
58+
return acumulador;
59+
return reverseRecursiveTail(text.substring(1), text.charAt(0) + acumulador);
60+
}
61+
62+
}

0 commit comments

Comments
 (0)