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

  • SEARCH
  • Home
  • 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 7775533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T17:39:20+00:00 2026-06-01T17:39:20+00:00

For my program I need to make a list of lists, with each sublist

  • 0

For my program I need to make a list of lists, with each sublist containing 2 numbers, X and Y along with the sum and product of these 2 numbers.
So far I have the following:

genList(95, X,[]):-!.
genList(N, X,[[X,Y,Sum,Product]|Xs]):-
    Y is N+1,
    Sum is X+Y,
    Sum<101,
    Product is X*Y,
    N1 is N+1,
    genList(N1, X,Xs).

This works just fine for my test case of genList(5,5,Q).
However, I’m having trouble making it work for any starting number.

The goal is to find every pair of numbers where sum<= 100. So running through the above for one starting value, X would find every pair 1 < X < Y, where sum<=100, and running through it with all numbers 2-N would give a complete list of possible pairs.

For those interested, the problem I’m working through is the sum/product problem, described here (Second on the page)

If anyone could help with this it would be greatly appreciated!

Also, no built in prolog predicates are able to be used, hence the complicated way of doing this rather than with a findall.

A small extract of the output produced by this predicated is as follows:

[[5,6,11,30],[5,7,12,35],[5,8,13,40],[5,9,14,45],[5,10,15,50],[5,11,16,55],[5,12,17,60],[5,13,18,65],[5,14,19,70],[5,15,20,75],[5,16,21,80],[5,17,22,85],[5,18,23,90],[5,19,24,95],[5,20,25,100],[5,21,26,105],[5,22,27,110], …

EDIT:

Ok, so after some editing, here is the latest version of my code.

I think it’s very close, but there’s still something not quite right.

It cycles through number pairs, but requires the use of “;” to view all the answers, which isn’t what I want. Additionally, it returns false after all the answers are exhausted. I just can’t figure it out.

Also, it gives a complete answer in the middle, but then removes a sublist each time until I’m left with only the last set of pairs.

E.g. genList(0,48,48,Q). gives me:

[[48,49,97,2352],[48,50,98,2400],[48,51,99,2448],[48,52,100,2496]]
[[48,49,97,2352],[48,50,98,2400],[48,51,99,2448],[48,52,100,2496],[49,50,99,2450],[49,51,100,2499]]
[[48,49,97,2352],[48,50,98,2400],[48,51,99,2448],[49,50,99,2450],[49,51,100,2499]]
[[48,49,97,2352],[48,50,98,2400],[49,50,99,2450],[49,51,100,2499]]
[[48,49,97,2352],[49,50,99,2450],[49,51,100,2499]]
[[49,50,99,2450],[49,51,100,2499]]
false.

As you can see, a sublist gets removed each time, I just can’t see why!

  • 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-01T17:39:21+00:00Added an answer on June 1, 2026 at 5:39 pm

    You can exploit Prolog backtracking here. Just state what you want. For example you could say:

    • I want X to be between 1 and 100.
    • I want Y to be between 1 and min(100 - X, X).
    • then I want their pair

    Let’s look at what a validPair/1 predicate would look like:

    validPair(X-Y) :-
        between(1, 100, X),
        Limit is min(100 - X, X),
        between(1, Limit, Y).
    

    You can just call it with

    ?- validPair(X).
    

    and browse results with ;, or build a list of all the matching pairs with findall/3.

    Edit: even with recursion, we can keep our statements:

    • I want X to be between 1 and 100.
    • I want Y to be between 1 and min(100 - X, X).
    • then I want their pair

    So, an idea to do it would be to set up a worker predicate:

    validPair(Result) :-
        validPair(0, 0, Result).
    validPair(X, Y, R) :-
        ...
    

    then set up the base case:

    validPair(101, _Y, []) :- !.
    

    and in the worker predicate, to implement the statements we made with some conditions:

    validPair(X, Y, [SomeStuff|R]) :-
        X =< 100,
        Limit is min(100 - X, X),
        Y =< Limit,
        !,
        % we can go on and increment Y once we're finished
        validPair(X, NextY, R).
    validPair(X, Y, R) :-
        % if we come here that means that Y is finished growing and
        % we have to increment X
        NextX is X + 1,
        validPair(NextX, 0, R).
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to make the program which have one form that contains PNG image
I need to make a program that adds long integers without using the biginteger
I need to make a java program with a security and a login system
So basically I'm making a program in C# and I need to make a
I need to be able to make a program that looks through a mailbox
In my program I need to programmatically configure an ApplicationContext. Specifically, I have a
Possible Duplicate: Does std::list::remove method call destructor of each removed element? I have a
I have a program (C#) with a list of tests to do. Also, I
I need some advice with the Java Print API. I have a program that
I need to a make a dialog appear as soon as my program's user

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.