📌  相关文章
📜  来自给定数组的奇数索引的所有合成数的总和

📅  最后修改于: 2021-09-06 05:25:34             🧑  作者: Mango

给定一个大小为 N 的数组 arr[] ,其中至少包含一个合数。任务是找出数组中奇数索引(其中索引是基于 1)的所有合数的总和。
例子:

方法:
为了解决上面提到的问题,主要思想是检查合数,也就是说,如果一个整数有两个以上的因数,那么它就是一个合数,否则不是。然后我们将迭代并检查数组奇数索引中的合数。如果它是一个合数,那么我们将把它加到sum变量中,否则跳过它直到数组的末尾。
下面是上述方法的实现:

C++
// C++ implementation to find the sum of all the
// composite numbers from odd indices of the given array
#include
using namespace std;
 
// Function to check for composite numbers
int composite(int n){
    int flag = 0;
   
    int c = 0;
     
    // Check if the factors are greater than 2
    for (int j = 1; j <= n; j++){
      if (n % j == 0){
            c += 1; 
      }   
    }        
            
    // Check if the number is composite or not
    if (c >= 3)
      flag = 1;
         
    return flag;
}
     
// Function to print the sum of all
// composite numbers in the array
void odd_indices(int arr[],int n){
     
int sum = 0;
     
// Iterate for odd indices in the array
for (int k = 0; k < n; k += 2){
    int check = composite(arr[k]);
         
    // Check if the number is composite
    // then add it to sum
    if (check == 1)
        sum += arr[k];
}
         
    // return the sum
    cout << sum << endl;
}
 
// Driver code
int main(){
    int arr[] = {13, 5, 8, 16, 25};
    int n = sizeof(arr)/sizeof(arr[0]);
    odd_indices(arr,n);
}
 
// This code is contributed by Surendra_Gangwar


Java
// Java implementation to find the sum of all the
// composite numbers from odd indices of the given array
 
class GFG{
 
// Function to check for composite numbers
static int composite(int n){
    int flag = 0;
     
    int c = 0;
     
    // Check if the factors are greater than 2
    for (int j = 1; j <= n; j++){
    if (n % j == 0){
            c += 1;
    }
    }        
         
    // Check if the number is composite or not
    if (c >= 3)
    flag = 1;
         
    return flag;
}
     
// Function to print the sum of all
// composite numbers in the array
static void odd_indices(int arr[],int n){
     
int sum = 0;
     
// Iterate for odd indices in the array
for (int k = 0; k < n; k += 2){
    int check = composite(arr[k]);
         
    // Check if the number is composite
    // then add it to sum
    if (check == 1)
        sum += arr[k];
}
         
    // return the sum
    System.out.print(sum +"\n");
}
 
// Driver code
public static void main(String[] args){
 
    int arr[] = {13, 5, 8, 16, 25};
    int n = arr.length;
    odd_indices(arr,n);
}
}
// This code contributed by sapnasingh4991


Python3
# Python3 implementation to find the sum of all the
# composite numbers from odd indices of the given array
 
# Function to print the sum of all
# composite numbers in the array
def odd_indices(arr):
     
    sum = 0
     
    # Iterate for odd indices in the array
    for k in range (0, len(arr), 2):
         
        check = composite (arr[k])
         
        # Check if the number is composite
        # then add it to sum
        sum += arr[k] if check == 1 else 0
         
    # return the sum
    print (sum)
 
# Function to check for composite numbers
def composite(n):
     
    flag = 0
     
    c = 0
     
    # Check if the factors are greater than 2
    for j in range (1, n + 1):
         
        if (n % j == 0):
             
            c += 1
     
    # Check if the number is composite or not
    if (c >= 3):
        flag = 1
         
    return flag
 
# Driver code
if __name__ == "__main__":
     
    arr = [13, 5, 8, 16, 25]
 
    odd_indices(arr)


C#
// C# implementation to find the sum
// of all the composite numbers from
// odd indices of the given array
using System;
 
class GFG{
 
// Function to check for composite numbers
static int composite(int n)
{
    int flag = 0;
    int c = 0;
     
    // Check if the factors are greater than 2
    for(int j = 1; j <= n; j++)
    {
       if (n % j == 0)
       {
           c += 1;
       }
    }        
         
    // Check if the number is composite or not
    if (c >= 3)
        flag = 1;
    return flag;
}
     
// Function to print the sum of all
// composite numbers in the array
static void odd_indices(int []arr,int n)
{
    int sum = 0;
     
    // Iterate for odd indices in the array
    for(int k = 0; k < n; k += 2)
    {
       int check = composite(arr[k]);
        
       // Check if the number is composite
       // then add it to sum
       if (check == 1)
           sum += arr[k];
    }
     
    // return the sum
    Console.Write(sum + "\n");
}
 
// Driver code
public static void Main(String[] args)
{
    int []arr = { 13, 5, 8, 16, 25 };
    int n = arr.Length;
     
    odd_indices(arr, n);
}
}
 
// This code is contributed by Rohit_ranjan


Javascript


输出:
33

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live