[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Recursion

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 14
Thread images: 3

File: Recursive Drawing-7.png (708KB, 670x505px) Image search: [Google]
Recursive Drawing-7.png
708KB, 670x505px
Show me your best recursive functions.

private static int pow(int x, int n) {
if (n == 0) {
return 1;
} else if (n % 2 == 0) {
return pow(x*x, n/2);
}
return pow(x*x, n/2)*x;
}
>>
    private static int fib(int n) {
if (n <= 2) return 1;
return fib (n-1) + fib(n-2);
}
>>
int ack(int m, int n)
{
if (!m) return n + 1;
if (!n) return ack(m - 1, 1);
return ack(m - 1, ack(m, n - 1));
}
>>
cmon guys

      private static HashSet<String> combinations(String s, int c) {
HashSet<String> combs = new HashSet<String>();
if(s.equals("")) {
return null;
} else if(s.length() == c) {
combs.add(s);
return combs;
}
for(int i = 0; i < s.length(); i++) {
combs.addAll(combinations(s.substring(0, i) + s.substring(i+1), c));
}
return combs;
}

private static HashSet<String> permutations(String s, String prefix) {
HashSet<String> perms = new HashSet<String>();
if(s.length() == 0) {
perms.add(prefix);
return perms;
}
for(int i = 0; i < s.length(); i++) {
perms.addAll(permutations(s.substring(0,i) + s.substring(i+1), prefix + s.charAt(i)));
}
return perms;
}
>>
>>57494502
implying half of gee could even write a recursive function
>>
here's a nice way to reverse a LL.

public ListNode reverseList(ListNode head) {
return reverseListUtil(head, null);
}

private ListNode reverseListUtil(ListNode root, ListNode prev) {
if (root == null) return prev;

ListNode temp = root.next;
root.next = prev;
return reverseListUtil(temp, root);
}
>>
File: IMG_20161022_110340.jpg (28KB, 302x389px) Image search: [Google]
IMG_20161022_110340.jpg
28KB, 302x389px
>>57494394
>>57494430
>>57494502
>not tail recursive
>>
File: Fibonachi.gif (2KB, 174x47px) Image search: [Google]
Fibonachi.gif
2KB, 174x47px
>>57494430
Not using this
>>
>>57494626
Here you are
let pow a b =
let rec loop accu a = function
| 0 -> accu
| b ->
let accu =
if b mod 2 = 0 then
accu
else
a * accu in
loop accu (a * a) (b / 2) in
loop 1 a b
;;
>>
>>57494394
public static bool rec() {
if(Math.random() < 0.0005) {
return true;
}
return rec();
}


Will this code halt?
If it halts what will it return?
>>
I can never think recursively.
>>
>>57497755
I think it will go to a loop for ever because I am pretty sure that Math.random() generates integers which you are comparing it to a different data type
>>
>>57494394
int willy()
{
return willy() + 1;
}
>>
>>57499009
>Math.random() generates integers


>public static double random()

>Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0. Returned values are chosen pseudorandomly with (approximately) uniform distribution from that range.
Thread posts: 14
Thread images: 3


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.