Why does my Hailstone Sequence function using recursion only output two values?
up vote
2
down vote
favorite
I have the following code:
fn hailSeq(number: i32) -> Vec<i32>
let mut vec = Vec::new();
vec.push(number);
if number == 1
vec.push(1);
return vec;
if number % 2 == 0
let num = number / 2;
vec.push(num);
hailSeq(num);
else
let num = 3 * number + 1;
vec.push(num);
hailSeq(num);
return vec;
It calculates the Hailstone sequence and stops at 1. The output should look like this for hailSeq(11):
[11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1]
However, my output looks like this:
[11, 34]
I am not really sure why this is occurring. Perhaps there is a limit on recursion in Rust that I don't know about, but I'm sure there's probably just an error in my code.
recursion rust
add a comment |
up vote
2
down vote
favorite
I have the following code:
fn hailSeq(number: i32) -> Vec<i32>
let mut vec = Vec::new();
vec.push(number);
if number == 1
vec.push(1);
return vec;
if number % 2 == 0
let num = number / 2;
vec.push(num);
hailSeq(num);
else
let num = 3 * number + 1;
vec.push(num);
hailSeq(num);
return vec;
It calculates the Hailstone sequence and stops at 1. The output should look like this for hailSeq(11):
[11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1]
However, my output looks like this:
[11, 34]
I am not really sure why this is occurring. Perhaps there is a limit on recursion in Rust that I don't know about, but I'm sure there's probably just an error in my code.
recursion rust
1
Please userustfmtto format your code properly next time. Second, in rust we do not use camelCase, but snake_case, e.g. your function name should be hail_seq. Third, don't use an explicitreturnfor the last statement, that is very unrusty. Just writevec(withouth return and;)
– hellow
Nov 9 at 12:51
1
You may explain or link how to generate a hail sequence.
– hellow
Nov 9 at 12:52
1
The more idiomatic in Rust is to use an iterator.
– Boiethios
Nov 9 at 13:10
The fun is there is an implementation for Rust on the link that I added rosettacode.org/wiki/Hailstone_sequence#Rust ;)
– Stargateur
Nov 9 at 13:10
See also stackoverflow.com/a/52905147/279627
– Sven Marnach
Nov 9 at 13:39
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have the following code:
fn hailSeq(number: i32) -> Vec<i32>
let mut vec = Vec::new();
vec.push(number);
if number == 1
vec.push(1);
return vec;
if number % 2 == 0
let num = number / 2;
vec.push(num);
hailSeq(num);
else
let num = 3 * number + 1;
vec.push(num);
hailSeq(num);
return vec;
It calculates the Hailstone sequence and stops at 1. The output should look like this for hailSeq(11):
[11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1]
However, my output looks like this:
[11, 34]
I am not really sure why this is occurring. Perhaps there is a limit on recursion in Rust that I don't know about, but I'm sure there's probably just an error in my code.
recursion rust
I have the following code:
fn hailSeq(number: i32) -> Vec<i32>
let mut vec = Vec::new();
vec.push(number);
if number == 1
vec.push(1);
return vec;
if number % 2 == 0
let num = number / 2;
vec.push(num);
hailSeq(num);
else
let num = 3 * number + 1;
vec.push(num);
hailSeq(num);
return vec;
It calculates the Hailstone sequence and stops at 1. The output should look like this for hailSeq(11):
[11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1]
However, my output looks like this:
[11, 34]
I am not really sure why this is occurring. Perhaps there is a limit on recursion in Rust that I don't know about, but I'm sure there's probably just an error in my code.
recursion rust
recursion rust
edited Nov 9 at 14:58
Shepmaster
142k11265398
142k11265398
asked Nov 9 at 12:47
SeePlusPlus
325
325
1
Please userustfmtto format your code properly next time. Second, in rust we do not use camelCase, but snake_case, e.g. your function name should be hail_seq. Third, don't use an explicitreturnfor the last statement, that is very unrusty. Just writevec(withouth return and;)
– hellow
Nov 9 at 12:51
1
You may explain or link how to generate a hail sequence.
– hellow
Nov 9 at 12:52
1
The more idiomatic in Rust is to use an iterator.
– Boiethios
Nov 9 at 13:10
The fun is there is an implementation for Rust on the link that I added rosettacode.org/wiki/Hailstone_sequence#Rust ;)
– Stargateur
Nov 9 at 13:10
See also stackoverflow.com/a/52905147/279627
– Sven Marnach
Nov 9 at 13:39
add a comment |
1
Please userustfmtto format your code properly next time. Second, in rust we do not use camelCase, but snake_case, e.g. your function name should be hail_seq. Third, don't use an explicitreturnfor the last statement, that is very unrusty. Just writevec(withouth return and;)
– hellow
Nov 9 at 12:51
1
You may explain or link how to generate a hail sequence.
– hellow
Nov 9 at 12:52
1
The more idiomatic in Rust is to use an iterator.
– Boiethios
Nov 9 at 13:10
The fun is there is an implementation for Rust on the link that I added rosettacode.org/wiki/Hailstone_sequence#Rust ;)
– Stargateur
Nov 9 at 13:10
See also stackoverflow.com/a/52905147/279627
– Sven Marnach
Nov 9 at 13:39
1
1
Please use
rustfmt to format your code properly next time. Second, in rust we do not use camelCase, but snake_case, e.g. your function name should be hail_seq. Third, don't use an explicit return for the last statement, that is very unrusty. Just write vec (withouth return and ;)– hellow
Nov 9 at 12:51
Please use
rustfmt to format your code properly next time. Second, in rust we do not use camelCase, but snake_case, e.g. your function name should be hail_seq. Third, don't use an explicit return for the last statement, that is very unrusty. Just write vec (withouth return and ;)– hellow
Nov 9 at 12:51
1
1
You may explain or link how to generate a hail sequence.
– hellow
Nov 9 at 12:52
You may explain or link how to generate a hail sequence.
– hellow
Nov 9 at 12:52
1
1
The more idiomatic in Rust is to use an iterator.
– Boiethios
Nov 9 at 13:10
The more idiomatic in Rust is to use an iterator.
– Boiethios
Nov 9 at 13:10
The fun is there is an implementation for Rust on the link that I added rosettacode.org/wiki/Hailstone_sequence#Rust ;)
– Stargateur
Nov 9 at 13:10
The fun is there is an implementation for Rust on the link that I added rosettacode.org/wiki/Hailstone_sequence#Rust ;)
– Stargateur
Nov 9 at 13:10
See also stackoverflow.com/a/52905147/279627
– Sven Marnach
Nov 9 at 13:39
See also stackoverflow.com/a/52905147/279627
– Sven Marnach
Nov 9 at 13:39
add a comment |
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
Your problem is not Rust-specific, but a more general problem.
On every call of hailSeq you create a new Vec every time, so that only the first vec (from the first call) would be used and returned, hence the [11, 34] (11 from the third line, 34 from the tenth line).
To fix this you have two options, I will provide one here.
The first one would be to extend the current vec with the returned vec, e.g. myvec.extend_from_slice(&returned_vec).
The second solution involves creating a vec on startup and passing the same instance to every call of the function.
fn hail_seq(number: i32) -> Vec<i32>
fn inner(number: i32, vec: &mut Vec<i32>)
vec.push(number);
if number == 1
return;
if number % 2 == 0
let num = number / 2;
inner(num, vec);
else
let num = 3 * number + 1;
inner(num, vec);
let mut v = vec!;
inner(number, &mut v);
v
fn main()
println!(":?", hail_seq(11));
(playground)
As a side-note: If you know that a number can't be negative, use a u32 instead because you will find errors at compile time instead of runtime.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
Your problem is not Rust-specific, but a more general problem.
On every call of hailSeq you create a new Vec every time, so that only the first vec (from the first call) would be used and returned, hence the [11, 34] (11 from the third line, 34 from the tenth line).
To fix this you have two options, I will provide one here.
The first one would be to extend the current vec with the returned vec, e.g. myvec.extend_from_slice(&returned_vec).
The second solution involves creating a vec on startup and passing the same instance to every call of the function.
fn hail_seq(number: i32) -> Vec<i32>
fn inner(number: i32, vec: &mut Vec<i32>)
vec.push(number);
if number == 1
return;
if number % 2 == 0
let num = number / 2;
inner(num, vec);
else
let num = 3 * number + 1;
inner(num, vec);
let mut v = vec!;
inner(number, &mut v);
v
fn main()
println!(":?", hail_seq(11));
(playground)
As a side-note: If you know that a number can't be negative, use a u32 instead because you will find errors at compile time instead of runtime.
add a comment |
up vote
3
down vote
accepted
Your problem is not Rust-specific, but a more general problem.
On every call of hailSeq you create a new Vec every time, so that only the first vec (from the first call) would be used and returned, hence the [11, 34] (11 from the third line, 34 from the tenth line).
To fix this you have two options, I will provide one here.
The first one would be to extend the current vec with the returned vec, e.g. myvec.extend_from_slice(&returned_vec).
The second solution involves creating a vec on startup and passing the same instance to every call of the function.
fn hail_seq(number: i32) -> Vec<i32>
fn inner(number: i32, vec: &mut Vec<i32>)
vec.push(number);
if number == 1
return;
if number % 2 == 0
let num = number / 2;
inner(num, vec);
else
let num = 3 * number + 1;
inner(num, vec);
let mut v = vec!;
inner(number, &mut v);
v
fn main()
println!(":?", hail_seq(11));
(playground)
As a side-note: If you know that a number can't be negative, use a u32 instead because you will find errors at compile time instead of runtime.
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
Your problem is not Rust-specific, but a more general problem.
On every call of hailSeq you create a new Vec every time, so that only the first vec (from the first call) would be used and returned, hence the [11, 34] (11 from the third line, 34 from the tenth line).
To fix this you have two options, I will provide one here.
The first one would be to extend the current vec with the returned vec, e.g. myvec.extend_from_slice(&returned_vec).
The second solution involves creating a vec on startup and passing the same instance to every call of the function.
fn hail_seq(number: i32) -> Vec<i32>
fn inner(number: i32, vec: &mut Vec<i32>)
vec.push(number);
if number == 1
return;
if number % 2 == 0
let num = number / 2;
inner(num, vec);
else
let num = 3 * number + 1;
inner(num, vec);
let mut v = vec!;
inner(number, &mut v);
v
fn main()
println!(":?", hail_seq(11));
(playground)
As a side-note: If you know that a number can't be negative, use a u32 instead because you will find errors at compile time instead of runtime.
Your problem is not Rust-specific, but a more general problem.
On every call of hailSeq you create a new Vec every time, so that only the first vec (from the first call) would be used and returned, hence the [11, 34] (11 from the third line, 34 from the tenth line).
To fix this you have two options, I will provide one here.
The first one would be to extend the current vec with the returned vec, e.g. myvec.extend_from_slice(&returned_vec).
The second solution involves creating a vec on startup and passing the same instance to every call of the function.
fn hail_seq(number: i32) -> Vec<i32>
fn inner(number: i32, vec: &mut Vec<i32>)
vec.push(number);
if number == 1
return;
if number % 2 == 0
let num = number / 2;
inner(num, vec);
else
let num = 3 * number + 1;
inner(num, vec);
let mut v = vec!;
inner(number, &mut v);
v
fn main()
println!(":?", hail_seq(11));
(playground)
As a side-note: If you know that a number can't be negative, use a u32 instead because you will find errors at compile time instead of runtime.
edited Nov 9 at 15:00
Shepmaster
142k11265398
142k11265398
answered Nov 9 at 12:59
hellow
3,69812041
3,69812041
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53225992%2fwhy-does-my-hailstone-sequence-function-using-recursion-only-output-two-values%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Please use
rustfmtto format your code properly next time. Second, in rust we do not use camelCase, but snake_case, e.g. your function name should be hail_seq. Third, don't use an explicitreturnfor the last statement, that is very unrusty. Just writevec(withouth return and;)– hellow
Nov 9 at 12:51
1
You may explain or link how to generate a hail sequence.
– hellow
Nov 9 at 12:52
1
The more idiomatic in Rust is to use an iterator.
– Boiethios
Nov 9 at 13:10
The fun is there is an implementation for Rust on the link that I added rosettacode.org/wiki/Hailstone_sequence#Rust ;)
– Stargateur
Nov 9 at 13:10
See also stackoverflow.com/a/52905147/279627
– Sven Marnach
Nov 9 at 13:39