Any way to make this code take up even less lines?
Show older comments
clear;
close all;
clc;
iif = @(varargin) varargin{2 * find([varargin{1:2:end}], 1, 'first')}();
d = @(x)mod(x,2);
e = @(x)x/2;
o = @(x)(3*x+1);
r = @(x)iif(d(x)==0,true,d(x)==1,false);
members = 'Enter an integer: ';
members=input(members);
while members(end) > 1 % run the loop
members = [members,iif(r(members(end)),e(members(end)),true,o(members(end)))];
end
disp(['The sequence starting with ' num2str(members(1)) ' will have ' num2str(numel(members)-1) ' members.']);%output
Answers (1)
Image Analyst
on 25 Jan 2019
You could combine 2 into 1:
members=input('Enter an integer ');
As someone whose programs typically are a few thousand lines long, I'm curious why shortening a 15 line long program is important to you.
Categories
Find more on Function Creation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!