Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8953233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:03:20+00:00 2026-06-15T14:03:20+00:00

I am trying to create a function called collatz_list in Prolog. This function takes

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:03:21+00:00Added an answer on June 15, 2026 at 2:03 pm

    Assuming you want to write a collatz_list/2 predicate with parameters (int, list), where list is the collatz sequence starting with int and eventually ending with 1 (we hope so! It’s an open problem so far); you just have to code the recursive definition in the declarative way.

    Here’s my attempt:

    /* if N = 1, we just stop */
    collatz_list(1, []).
    
    /* case 1: N even
       we place N / 2 in the head of the list
       the tail is the collatz sequence starting from N / 2 */
    collatz_list(N, [H|T]) :-
        0 is N mod 2,
        H is N / 2, 
        collatz_list(H, T), !. 
    
    /* case 2: N is odd
       we place 3N + 1 in the head of the list
       the tail is the collatz sequence starting from 3N + 1 */
    collatz_list(N, [H|T]) :-
        H is 3 * N + 1, 
        collatz_list(H, T). 
    

    Modified version, includes starting number

    Let’s test it:

    full_list(N, [N|T]) :-
        collatz_list(N, T).
    
    collatz_list(1, []).
    
    collatz_list(N, [H|T]) :-
        0 is N mod 2,
        H is N / 2, 
        collatz_list(H, T), !. 
    
    collatz_list(N, [H|T]) :-
        H is 3 * N + 1, 
        collatz_list(H, T). 
    
    ?- full_list(27, L).
    L = [27, 82, 41, 124, 62, 31, 94, 47, 142|...].
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a java function where it takes 2 parameter. One
i'm trying to create my first jQuery plugin. i got a prototype function called
I'm trying to create a function called saveorder . This is my code so
What I am trying to do is create a function that will be called
I'm trying to create a function that would append two vectors together, but I
I am trying to create a function that given a divid, and a list
I'm trying to create a DLL that exports a function called GetName. I'd like
I'm writing my first wordpress plugin and I'm trying to create a function to
I'm trying to create a function (in Python) that takes its input (a chemical
I'm trying to create a random quote generator. I have created a function called

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.