SlideShare una empresa de Scribd logo
1 de 8
CATALAN NUMBERS
от Виктор Асенов
КАКВО СА ТЕ?
• В комбинаторната математика каталaнските числа са
поредица от естествени числа, които се срещат в
различни проблеми с броенето, често включващи
рекурсивно дефинирани обекти. Те са кръстени на
френско-белгийския математик Йожен Шарл Каталан
(1814–1894).
ФОРМУЛА
• Cn = (2 * n)! / ((n + 1)! * n!)
• Първите числа на Каталан за n = 0, 1, 2, 3 … са:
1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58785, ...
АПЛИКАЦИЯ В КОМБИНАТОРИКА
• В комбинаториката има много проблеми с броенето,
чието решение се дава от числата на Каталан. Книгата
Enumerative Combinatorics: Volume 2 от
комбинаторика Richard P. Stanley съдържа набор от
упражнения, които описват 66 различни
интерпретации на числата на Каталан. Следват някои
примери с илюстрации на случая C3 = 5
DYCK WORDS
• Cn е броят на думите на Dyck с дължина 2n. Думата
на Dyck е низ, състоящ се от n X и n Y, така че нито
един начален сегмент от низа няма повече X от Y.
Пример за думите на Dyck с дължина 6:
XXXYYY XYXXYY XYXYXY XXYYXY XXYXYY
• Ако заместим X с отварящи скоби, а Y със затварящи,
виждаме, че скобите си съвпадат:
((())) ()(()) ()()() (())() (()())
ЗАВЪРШЕНИ СКОБИ
• Cn е броят на различните начини, по които n + 1
фактора могат да бъдат напълно поставени в скоби. За
n = 3, например, имаме следните пет различни
поставяния в скоби на четири фактора:
((ab)c)d (a(bc))d (ab)(cd) a((bc)d) a(b(cd))
ПЪЛНИ ДВОИЧНИ ДЪРВЕТА
• Чрез каталанските числа може да покажем броят на
пълните двоични дървета с n + 1 листа или,
еквивалентно, с общо n вътрешни възли:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class CatalanNumbers {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = Integer.parseInt(scanner.nextLine());
List<Long> catalanNumbers = getCatalanNumbers(new ArrayList<>(List.of(1L)), n);
System.out.println(catalanNumbers);
}
public static List<Long> getCatalanNumbers(List<Long> catalanNumbers, int n) {
if(catalanNumbers.size() == (n + 1)) {
return catalanNumbers;
}
long currentCatalanNumber = factorial(2 * catalanNumbers.size()) /
(factorial(catalanNumbers.size() + 1) * factorial(catalanNumbers.size()));
catalanNumbers.add(currentCatalanNumber);
return getCatalanNumbers(catalanNumbers, n);
}
public static long factorial(int num) {
if(num == 0) {
return 1;
} else {
return num * factorial(num - 1);
}
}
}
ПРИМЕРЕН КОД С РЕКУРСИЯ

Más contenido relacionado

Destacado

Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Saba Software
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming Language
Simplilearn
 

Destacado (20)

How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them wellGood Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
 
Introduction to C Programming Language
Introduction to C Programming LanguageIntroduction to C Programming Language
Introduction to C Programming Language
 

Catalan Numbers

  • 2. КАКВО СА ТЕ? • В комбинаторната математика каталaнските числа са поредица от естествени числа, които се срещат в различни проблеми с броенето, често включващи рекурсивно дефинирани обекти. Те са кръстени на френско-белгийския математик Йожен Шарл Каталан (1814–1894).
  • 3. ФОРМУЛА • Cn = (2 * n)! / ((n + 1)! * n!) • Първите числа на Каталан за n = 0, 1, 2, 3 … са: 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862, 16796, 58785, ...
  • 4. АПЛИКАЦИЯ В КОМБИНАТОРИКА • В комбинаториката има много проблеми с броенето, чието решение се дава от числата на Каталан. Книгата Enumerative Combinatorics: Volume 2 от комбинаторика Richard P. Stanley съдържа набор от упражнения, които описват 66 различни интерпретации на числата на Каталан. Следват някои примери с илюстрации на случая C3 = 5
  • 5. DYCK WORDS • Cn е броят на думите на Dyck с дължина 2n. Думата на Dyck е низ, състоящ се от n X и n Y, така че нито един начален сегмент от низа няма повече X от Y. Пример за думите на Dyck с дължина 6: XXXYYY XYXXYY XYXYXY XXYYXY XXYXYY • Ако заместим X с отварящи скоби, а Y със затварящи, виждаме, че скобите си съвпадат: ((())) ()(()) ()()() (())() (()())
  • 6. ЗАВЪРШЕНИ СКОБИ • Cn е броят на различните начини, по които n + 1 фактора могат да бъдат напълно поставени в скоби. За n = 3, например, имаме следните пет различни поставяния в скоби на четири фактора: ((ab)c)d (a(bc))d (ab)(cd) a((bc)d) a(b(cd))
  • 7. ПЪЛНИ ДВОИЧНИ ДЪРВЕТА • Чрез каталанските числа може да покажем броят на пълните двоични дървета с n + 1 листа или, еквивалентно, с общо n вътрешни възли:
  • 8. import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class CatalanNumbers { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = Integer.parseInt(scanner.nextLine()); List<Long> catalanNumbers = getCatalanNumbers(new ArrayList<>(List.of(1L)), n); System.out.println(catalanNumbers); } public static List<Long> getCatalanNumbers(List<Long> catalanNumbers, int n) { if(catalanNumbers.size() == (n + 1)) { return catalanNumbers; } long currentCatalanNumber = factorial(2 * catalanNumbers.size()) / (factorial(catalanNumbers.size() + 1) * factorial(catalanNumbers.size())); catalanNumbers.add(currentCatalanNumber); return getCatalanNumbers(catalanNumbers, n); } public static long factorial(int num) { if(num == 0) { return 1; } else { return num * factorial(num - 1); } } } ПРИМЕРЕН КОД С РЕКУРСИЯ