collatz

How to avoid stackoverflow at collatz for high values?

本小妞迷上赌 提交于 2019-12-12 03:38:43
问题 I'm working on an eulers problem, where you have to get the longest collatz chain. The problem is, you have to find it in a sequence from 1 to 1 000 000. My code works great up to 100 000, then it throws StackOverFlowError . Can I avoid it, or is my code crap? public class Collatz { private static final ArrayList<Integer> previous = new ArrayList<>(); public static void main(String[] args) { for (int i = 1; i < 100000; i++){ collatzLength(i, 1); } Integer i = Collections.max(previous); System

Haskell : Problem converting result of division to integral type

情到浓时终转凉″ 提交于 2019-12-11 05:38:47
问题 I'm learning Haskell and stuck trying to understand the type system. I'm trying to write a function which returns the length of the series 'Half or Three Plus One' for an input. Here's my attempt at the function, using a recursive approach (the function is valid for integral inputs only): hotpo :: (Integral a) => a->a hotpo n = hotpoHelper n 1 hotpoHelper:: (Integral a) => a->a->a hotpoHelper 1 n = n hotpoHelper num count | even num = hotpoHelper (truncate (num/2)) (count+1) | otherwise =

Too long runtime of euler project #14 in Java

半腔热情 提交于 2019-12-10 20:00:49
问题 My code of Euler Project 14 is below. I have run this code for more than 3 hours with output result and it seems to be infinite. When I test a single number such as 11, 27, it will output the collatz chain number:14 and 111 quickly. But I don't know why it couldn't output 1000000 number's max chain number. /*Problem 14 * The following iterative sequence is defined for the set of positive integers: * n -> n/2 (n is even) * n -> 3n + 1 (n is odd) * Using the rule above and starting with 13, we

collatz-list implementation using Prolog

馋奶兔 提交于 2019-12-10 18:09:42
问题 I am trying to create a function called collatz_list in Prolog. This function takes two arguments, the first one is a number and the second in a list. This list will be my output of this function. So, here's my function: collatz_list(1,[1]). collatz_list(N,[H|T]) :- N > 1, N mod 2 =:= 0, collatz_list(N, [H|T]). collatz_list(N,[H|T]) :- N > 1, N mod 2 =:= 1, N is N*3 +1, collatz_list(N,[H|T]). I am struggling with creating the output list. Can anyone help me on that? Thanks. 回答1: Assuming you

Memoization with recursive method in java

坚强是说给别人听的谎言 提交于 2019-12-10 17:18:54
问题 I am working on a homework assignment, and I have completely exhausted myself. I'm new to programming, and this is my first programming class. this is the problem: Consider the following recursive function in Collatz.java, which is related to a famous unsolved problem in number theory, known as the Collatz problem or the 3n + 1 problem. public static void collatz(int n) { StdOut.print(n + " "); if (n == 1) return; if (n % 2 == 0) collatz(n / 2); else collatz(3*n + 1);} For example, a call to

3n+1 challenge at UVa

旧城冷巷雨未停 提交于 2019-12-06 08:58:57
I'm having trouble running the "3n+1 Problem" from the "Programming Challenges" book . I've tried every solution in Java I could find on google (even the ones on Stack Overflow), and not a single one works, they all report a "Wrong answer". I also found a working C++ solution, translated it to Java, and same thing: "Wrong answer". I'm using the template from Programming Challenges for Java submissions , and I could swear my algorithm is right, the only possible problem I can think of is in the code for reading the input or writing the output, but I can't figure it out. Here's my code, any help

Why is this simple haskell algorithm so slow?

倖福魔咒の 提交于 2019-12-05 12:54:53
问题 Spoiler alert: this is related to Problem 14 from Project Euler. The following code takes around 15s to run. I have a non-recursive Java solution that runs in 1s. I think I should be able to get this code much closer to that. import Data.List collatz a 1 = a collatz a x | even x = collatz (a + 1) (x `div` 2) | otherwise = collatz (a + 1) (3 * x + 1) main = do print ((foldl1' max) . map (collatz 1) $ [1..1000000]) I have profiled with +RHS -p and noticed that the allocated memory is large, and

Error “No instance for (Num [t])” in Collatz function

你离开我真会死。 提交于 2019-12-01 20:28:51
I am new to Haskell, and programming in general. I am trying to define a function which generates the sequence of Collatz numbers from n . I have: collatz n = (collatz' n) : 1 where collatz' n = (takeWhile (>1) (collatz'' n)) where collatz'' n = n : collatz'' (collatz''' n) where collatz''' 1 = 1 collatz''' n = if (even n) then (div n 2) else ((3*2)+1) When I run this in GHCi, I get the error: No instance for (Num [t]) arising from the literal `2' at <interactive>:1:7 Possible fix: add an instance declaration for (Num [t]) I don't know what this means. The problem seems to be appending "1" to

Code Golf: Collatz Conjecture

旧时模样 提交于 2019-11-28 16:27:29
Inspired by http://xkcd.com/710/ here is a code golf for it. The Challenge Given a positive integer greater than 0, print out the hailstone sequence for that number. The Hailstone Sequence See Wikipedia for more detail.. If the number is even, divide it by two. If the number is odd, triple it and add one. Repeat this with the number produced until it reaches 1. (if it continues after 1, it will go in an infinite loop of 1 -> 4 -> 2 -> 1... ) Sometimes code is the best way to explain, so here is some from Wikipedia function collatz(n) show n if n > 1 if n is odd call collatz(3n + 1) else call

Implementing the collatz function using Python

守給你的承諾、 提交于 2019-11-28 12:26:40
I am currently having trouble completing this challenge in "Automate the boring stuff": My code is: def collatz(number): global seqNum if (seqNum % 2 == 0): return seqNum // 2 elif (seqNum % 2 == 1): return 3 * seqNum + 1 print('What number would you like to use?') seqNum = input() number = int(seqNum) i = number while i > 1: collatz(seqNum) print(number) And I am getting this error: "Traceback (most recent call last): File "C:/Users/Administrative/AppData/Local/Programs/Python/Python36-32/collatzSeq.py", line 15, in <module> collatz(seqNum) File "C:/Users/Administrative/AppData/Local/Programs